-1

I was wondering if there was a way to find an element on a webpage based on the value attribute.

<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:

driver.findElement(By.xpath("//*[contains(text(), '" + term + "')]"))
stackiee
  • 195
  • 1
  • 8

2 Answers2

0

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.

RKelley
  • 1,099
  • 8
  • 14
0

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:

  • :

    driver.findElement(By.cssSelector("option[value='abc']"))
    
  • :

    driver.findElement(By.xpath("//option[@value='abc']"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352