Home > Mobile >  Selenium Finding elements by class name and value in python
Selenium Finding elements by class name and value in python

Time:09-15

Is there a way to use driver.find_element(By.XPATH, ...) to find an element specifying NOT ONLY its class, but also the class value?

I know this:

driver.find_element(By.XPATH, "//*[@class='class_value'")

But how can I add the value filter?

CodePudding user response:

Sure, you can locate elements by any attributes including any possible combinations of them.
So, to locate element based on it class name attribute value and value attribute value it can be something like this

driver.find_element(By.XPATH, "//*[@class='class_value' and @value='value_value']")

In case there are other class attribute values or / and value attribute values we can use contains as following

driver.find_element(By.XPATH, "//*[contains(@class,'class_value') and contains(@value,'value_value')]")

Or (the same as above, just different syntax)

driver.find_element(By.XPATH, "//*[contains(@class,'class_value')][contains(@value,'value_value')]")
  • Related