简体   繁体   English

Selenium Webdriver-相对xpath,获取带有文本的元素

[英]Selenium webdriver - relative xpath, get element with text

I am using Selenium webdriver with java. 我在Java中使用Selenium webdriver。

I am trying to find elements in drop down lists, can`t access it by exact id/name/xpath because those are dynamic elements, so I tried to locate it by relative xpath, and it works but not fully, please see image below: 我正在尝试在下拉列表中查找元素,由于它们是动态元素,因此无法通过确切的id / name / xpath进行访问,因此我尝试通过相对xpath进行查找,但它可以正常运行,但不能完全使用,请参见下图:

IMAGE ON IMGUR (CANT POST IMAGES YES) IMGUR上的图像(无法发布图像)

As you can see on top left, there is a drop down overlay with 3 options, ALL/ACTIVE/INACTIVE, now I managed to locate all 3 of them by using relative xpath as shown on image: 正如您在左上角看到的那样,有一个带有3个选项ALL / ACTIVE / INACTIVE的下拉叠加层,现在我设法通过使用相对xpath来定位所有三个选项,如图所示:

//*[@class="cdk-overlay-pane"]//*[@class="mat-option-text"]

But how to now get one element (out of 3 in this case) that got specific text, for example "Active" ? 但是,现在如何获取一个具有特定文本的元素(在这种情况下为3个元素),例如“ Active”? I tried following: 我尝试了以下操作:

//*[@class="cdk-overlay-pane"]//*[@class="mat-option-text"]//*[contains(text(),'Active')]

But it does not find anything, any help would be welcome! 但是找不到任何东西,欢迎任何帮助!

Found a solution, search for text using "contains" 找到了解决方案,使用“包含”搜索文本

Here`s working relative xpath in my case: 这是我的情况下相对工作的xpath:

//*[@class="cdk-overlay-pane"]//*[contains(text(), 'All')]

It would return element with "All" text that is located within drop down panel. 它将返回位于下拉面板中的带有“所有”文本的元素。

试试看

//*[@class="cdk-overlay-pane"]//span[@class="mat-option-text" and normalize-space(text())="All"]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM