Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find_element_by_class_name for multiple classes [duplicate]

Selenium in the API for Python / Django has the function driver.find_element/elements_by_class_name (), but it is not written whether it can be used for several classes I need choose element with several classes like bj,bd,bi If possible, how?

like image 377
Kasper Avatar asked Jun 26 '17 12:06

Kasper


1 Answers

The answer is No, You can't use driver.find_element_by_class_name () or driver.find_elements_by_class_name () with multiple class names. It accepts only single class name.

However, you can use find_elements_by_xpath or find_element_by_css_selector method to achieve finding element with multiple class names.

for example below code will find element on google website using two different class names.

url= "http://google.com"
driver = webdriver.Chrome()
driver.get(url)
driver.find_elements_by_xpath("//*[@class='sfibbbc' or @class='jsb']")
# Following line will result in error 
driver.find_elements_by_class_name("sfibbbc jsb")
like image 57
Gaurang Shah Avatar answered Oct 24 '22 00:10

Gaurang Shah