[英]Find href attributes with find_elements_by_class_name in Selenium with Python
[英]Python/Selenium - find_elements_by_class_name with long class name
硒的基本问题。
我正在尝试遍历的站点中有一个类(没有ID,没有名称),但是类名非常长,例如class="my really long crappy class name with space"
我正在尝试mydriver.find_elements_by_class_name(idClass);
其中mydriver = webdriver.Firefox()
。
我也尝试了my.really.long.crappy.class.name.with.space
,这也行不通(将其作为对像我这样的其他问题的已发布解决方案。当我运行它时,我没有收到错误,但是我的结果是零号。
以下功能是我当前正在使用的功能,正在打印
def getElementByClass(idClass):
a=[];
try:
print idClass;
a = mydriver.find_elements_by_class_name(idClass);
return a;
except:
print 'crap';
您可以使用find_elements_by_css_selector()
:
mydriver.find_elements_by_css_selector('.class1.class2.class3');
其中class1
, class2
和class3
是元素具有的类名称。
或者,另一个选择是使用find_elements_by_xpath()
:
mydriver.find_elements_by_xpath('//*[@class="my really long crappy class name with space"]')
如果事先知道,也可以用标签名称替换*
。
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.