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?
Asked
Active
Viewed 4.1k times
16
-
use find_elements_by_class_name. – Vardhman Patil Jun 26 '17 at 12:45
-
1Did you try it? Please read [ask], especially the part about [mcve] (MCVE), and [How much research effort is expected?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) This will help you debug your own programs and solve problems for yourself. If you do this and are still stuck you can come back and post your MCVE, what you tried, and the execution result including any error messages so we can better help you. Also provide a link to the page and/or the relevant HTML. – JeffC Jun 26 '17 at 13:08
-
Yes, i try it. Different things. But I thought that there might be some function / reception not described in the documentation. – Kasper Jun 26 '17 at 13:12
-
You can use a loop and use .format() to get each class. – keramat Sep 25 '18 at 04:37
1 Answers
23
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")

Gaurang Shah
- 11,764
- 9
- 74
- 137
-
1I love programming for this. Tried only through classes and all. But it's even easier with xpath. Thank you – Kasper Jun 26 '17 at 12:59
-
1I find it's more concise with css selector (of course I'm with java )...findElements(By.cssSelector(".sfibbbc.jsb")) – 1234 Jun 26 '18 at 00:02
-
2FYI @1234's suggestion in python is the following: `driver.find_elements_by_css_selector(". sfibbbc. jsb")` – ClayHerendeen Apr 26 '20 at 03:56