kiks73
kiks73

Reputation: 3778

Selenium Finding elements by class name and value in python

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?

Upvotes: 1

Views: 661

Answers (1)

Prophet
Prophet

Reputation: 33361

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')]")

Upvotes: 2

Related Questions