简体   繁体   English

Selenium:按类xpath的findElements,在特定元素之前

[英]Selenium: findElements by class xpath, before a specific element

I have a page with a layout like this: 我的页面布局如下:

<div class="cool nice">...</div>  //1
<div class="cool nice">...</div>  //2
<div class="cool nice">...</div>  //3
<div class="cool nice">...</div>  //4
<div class="separator element">...</div>
<div class="cool nice">...</div>  //5
<div class="cool nice">...</div>  //6

I can get all elements with class="cool nice" like this: 我可以使用class="cool nice"获得所有元素,如下所示:

List<WebElement> elements=driver.findElements(By.xpath("//*[@class='cool nice']"));

However, I'd like to exclude elements that follow the element with class="separator element . What is the best way to do that? 但是,我想排除class="separator element ,最好的方法是什么?

Use preceding-sibling or preceding 使用preceding-siblingpreceding

//div[@class='separator element']/preceding::div[@class='cool nice']

OR 要么

//div[@class='separator element']/preceding-sibling::div[@class='cool nice']

Try now 现在试试

List<WebElement> elements=driver.findElements(By.xpath(" //div[@class='separator element']/preceding-sibling::div[@class='cool nice']"));

在我看来应该没有一个:

//*[@class="cool nice" and not(./preceding::*[@class="separator element"])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium WebDriver-使用findElements选择Webelements(By.xpath - Selenium WebDriver - Selecting webelements with findElements(By.xpath findelements中的硒(java)findelements - selenium (java) findelements in findelements Selenium findElements()/ findElement(按类)返回null - Selenium findElements() / findElement(by class) returns null Selenium:让findElements等待可见元素,尽管存在不可见元素 - Selenium: Let findElements wait for visible element although invisible element exists Selenium + Java = FindElements (list) 并检查其中一些是否包含特定文本 - Selenium + Java = FindElements (list) and check if some of them contains a specific text 如何使用 Selenium 中的 XPath 选择器查找特定 web 元素的特定子元素 - How to find a specific child of a specific web element with XPath selectors in Selenium Webdriver findElements由xpath提供 - Webdriver findElements By xpath Java Selenium WebElement element.findElements无法正常工作 - Java Selenium WebElement element.findElements not working as expected Selenium findElements()多次返回第一个元素的相同实例 - Selenium findElements() returns the same instance of the first element multiple times Selenium Webdrive(Java)-在xpath中找到没有特定属性的元素 - Selenium Webdrive (Java) - find an element in xpath where is no specific attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM