简体   繁体   English

Java Selenium - 根据值属性选择一个 WebElement

[英]Java Selenium - Select a WebElement based on value attribute

I was wondering if there was a way to find an element on a webpage based on the value attribute.我想知道是否有办法根据 value 属性在网页上查找元素。

<option value="abc">Some text here</option>
<option value="bcd">Some text here</option>

I figured I could just create a list of WebElements based on the tag name and traverse each one using .getAttribute("value"), but I was wondering if there was a more effective way of doing this similar to the way you can find an element based on its text using:我想我可以根据标签名称创建一个 WebElements 列表,并使用 .getAttribute("value") 遍历每个元素,但我想知道是否有更有效的方法来执行此操作,类似于您可以找到的方法元素基于其文本使用:

driver.findElement(By.xpath("//*[contains(text(), '" + term + "')]"))

You can do this:你可以这样做:

driver.findElement(By.cssSelector("[value=\"abc\"]"))

and you would change the value of value depending on what you were trying to find.并且您将根据您尝试查找的内容更改 value 的值。

To locate the element with value="abc" on a webpage `based on the value attribute of abc you can use either of the following Locator Strategies :要根据abcvalue属性在网页上使用value="abc"定位元素,您可以使用以下任一定位器策略

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

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