Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 如何选择div中没有特定类的元素_Selenium_Xpath_Selenium Webdriver_Css Selectors - Fatal编程技术网

Selenium 如何选择div中没有特定类的元素

Selenium 如何选择div中没有特定类的元素,selenium,xpath,selenium-webdriver,css-selectors,Selenium,Xpath,Selenium Webdriver,Css Selectors,我的html如下所示。我希望通过CSSselector或xpath访问那些没有div

我的html如下所示。我希望通过CSSselector或xpath访问那些没有div 锁定视频

<div class="item video " data-reactid="165">
    <a href="/video/peppa/xyz" data-reactid="166">
        <div class="LazyLoad is-visible" style="height:168px;" data-reactid="167">
            <img class="visual-video" src="https://abc.jpg?w=300" alt="I am locked video">
        </div>
        <p class="text" data-reactid="168">Episodio completo</p>
        <img class="video" src="/images/icon-video.svg" data-reactid="169">
        <div class="locked" data-reactid="170">
            <div class="opaque" data-reactid="171"></div>
            <p data-reactid="172">Activa tu cuenta</p>
        </div>
    </a>
    <p class="name" data-reactid="173">I am locked video</p>
</div>

我被锁定了视频

免费视频1

<div class="item video " data-reactid="185">
    <a href="/video/ghi" data-reactid="186">
        <div class="LazyLoad is-visible" style="height:168px;" data-reactid="187">
            <img class="visual-video" src="https://ghi.jpg?w=300" alt="I am free video1">
        </div>
        <p class="text" data-reactid="188">Episodio completo</p>
        <img class="video" src="/images/icon-video.svg" data-reactid="189">
    </a>
    <p class="name" data-reactid="190">I am free video1</p>
</div>

我有空1

免费视频2

<div class="item video " data-reactid="192">
    <a href="/video/sddfo" data-reactid="193">
        <div class="LazyLoad is-visible" style="height:168px;" data-reactid="194">
            <img class="video" src="/images/icon-video.svg" data-reactid="195">
        </div>
    </a>
    <p class="name" data-reactid="196">I am free video2</p>
</div>

我有空2


使用XPath执行此操作相对简单:

//div[contains(@class, 'video') and not(div[@class='locked'])]
请注意,严格来说,为了避免误报,您应该:


如果您的CSS实现支持
:not()
“pseudo element”选择器,那么您可以尝试

.item.video:not(div.locked)p.name

比照

这可能过于具体,因为我不理解加价的背景

对于XPath,我认为应该是这样的:


//div[contains(@class,'video')和not(.//div[contains(@class,'locked')])//p[contains(@class,'name')

这些文本是常量吗?如果是,则可以从文本中提取元素


这是一个有趣的问题,我想出了一个主意

我的想法是先捕获所有视频,然后从所有视频列表中删除锁定的视频。代码如下:

  //Gather all the videos
  List<WebElement> all = driver.findElements(By.xpath("//div[@class,'item video']/a"));

  //Gather the videos which has locked
  List<WebElement> locked = driver.findElements(By.xpath("//div[@class,'item video']//div[@class,'locked']"));

  //Remove the locked videos from all videos
  for(WebElement lock : locked)
  {
      all.remove(lock);
  }


  //Now "all" contains only the free videos.
  for(WebElement free : all)
  {
      //Do the stuff
  }
//收集所有视频
List all=driver.findElements(By.xpath(“//div[@class,'item video']/a”);
//收集已锁定的视频
List locked=driver.findElements(By.xpath(//div[@class,'item video']//div[@class,'locked']);
//从所有视频中删除锁定的视频
用于(WebElement锁定:锁定)
{
所有。移除(锁定);
}
//现在“全部”只包含免费视频。
for(WebElement-free:all)
{
//动手
}

注意-未测试。请检查并让我知道是否有效

我已经检查了以上这些解决方案,因为这些解决方案很长时间以来都可用,但它不起作用..item.video div:Not(.locked)这不起作用,因为它会发现div class=“LazyLoad可见,而div class=“不透明”“我误解了这个问题。你希望返回的(选定的)元素是什么?你希望“webelements”和你提到的文本。这是否意味着你想要
元素?如果是这样,@alexce的答案可能是最好的开始。
//div[contains(@class,'video')而不是(a/div[@class='locked'])/p[contains(@class,'name')
可能会这样做。特里奇正在查找没有锁定视频的webelements列表。然后我会单击该列表中的第一个。无论如何,我使用了xpath,如下所示,它工作了。//div[contains(@class,'item video')]/a[not(div[@class='locked'])和(div[@class='LazyLoad is visible'])和(img)[@class='video']]我已经尝试了以上这些解决方案,但它不起作用。我也尝试了类似于//a/div[contains(@class,'LazyLoad is visible')和not(contains(@class,'locked'))的方法,但没有起作用。我以前尝试过这个方法,但我使用了减法列表,因此放弃了。总之,我使用了xpath作为//div[contains](@class='item video')]/a[not(div[@class='locked'])和(div[@class='LazyLoad is visible'])和(img[@class='video'])”;“我尝试过的方法是:列出所有视频=驱动程序。findElements(By.cssSelector(Properties.videos));列出所有锁定视频=驱动程序。findElements(By.cssSelector(Properties.lockedvideos));列出所有免费视频=减去(所有视频,所有锁定视频);所有免费视频。获取(0)。单击();不确定您编写的css。它必须是img.videos和div.locked。您可以使用我提供的xpath进行检查。尝试打印每个列表中的元素数以进行调试。我正在查找没有锁定视频的webelements列表。然后我会单击该列表中的第一个。无论如何,我使用xpath作为它在下面工作。//div[contains(@class='item video')]/a[not(div[@class='locked'])和(div[@class='LazyLoad is visible'])和(img[@class='video'])]
  //Gather all the videos
  List<WebElement> all = driver.findElements(By.xpath("//div[@class,'item video']/a"));

  //Gather the videos which has locked
  List<WebElement> locked = driver.findElements(By.xpath("//div[@class,'item video']//div[@class,'locked']"));

  //Remove the locked videos from all videos
  for(WebElement lock : locked)
  {
      all.remove(lock);
  }


  //Now "all" contains only the free videos.
  for(WebElement free : all)
  {
      //Do the stuff
  }