find_element_by_class_name 用于多个类 [英] find_element_by_class_name for multiple classes

查看:392
本文介绍了find_element_by_class_name 用于多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 classes I need choose element with several classes like bj,bd,bi If 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_xpathfind_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 用于多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆