Selenium Finding elements by class name and value in python

Question:

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?

Asked By: kiks73

||

Answers:

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')]")
Answered By: Prophet
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.