问题描述
Selenium 在 API for Python/Django 中有函数 driver.find_element/elements_by_class_name(),但是没有写是否可以用于几个类我需要选择具有多个类的元素,例如 bj、bd、bi如果可能,怎么做?
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 classesI need choose element with several classes like bj,bd,biIf possible, how?
推荐答案
答案是否定的,你不能使用driver.find_element_by_class_name()
或driver.find_elements_by_class_name()
代码> 具有多个类名.它只接受单个类名.
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.
但是,您可以使用find_elements_by_xpath
或find_element_by_css_selector
方法来实现查找具有多个类名的元素.
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")
这篇关于find_element_by_class_name 用于多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!