[英]Loop through XPath element positions in Python using Selenium
How can I loop through element positions in Python using Selenium? 如何使用Selenium在Python中循环浏览元素位置?
I've tried this and it's not working: 我已经尝试过了,但是没有用:
for i in range(2,21):
un = browser.find_element_by_xpath("(//div[@class='cname'])[i]").text
print(un)
You have to format your string to put inside i
value: 您必须格式化字符串以放入
i
值:
for i in range(2,21):
un = browser.find_element_by_xpath("(//div[@class='cname'])[{0}]".format(i)).text
print(un)
As per your code block an alternative would be to to use find_elements_by_xpath
along with the index [i]
as follows : 根据您的代码块,另一种方法是使用
find_elements_by_xpath
以及索引[i]
,如下所示:
for i in range(2,21):
un = browser.find_elements_by_xpath("//div[@class='cname']")[i].text
print(un)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.