python - XPath index value iteration
问题描述
get_div1 = driver.find_element_by_xpath("(//div[@dojoattachpoint='titleNode'])[1]")
get_div2 = driver.find_element_by_xpath("(//div[@dojoattachpoint='titleNode'])[2]")
get_div3 = driver.find_element_by_xpath("(//div[@dojoattachpoint='titleNode'])[3]")
get_div4 = driver.find_element_by_xpath("(//div[@dojoattachpoint='titleNode'])[4]")
.
.
.
.
.
I am new to Python-Selenium. I get my required output when I use the Indexes... I am not sure at which Index i get my output so How can I iterate this? Since, I am finding difficulty to assign this //div[@dojoattachpoint='titleNode'])[4] to a variable and manipulate. and I do not know how many indexes are available in that web page.
解决方案
You can get all the elements matching this locator into list and then iterate over the list, something like this:
elements = driver.find_elements_by_xpath("//div[@dojoattachpoint='titleNode']")
for element in elements:
#do what you need here, like this
print(element.text)
推荐阅读
- service - Dynamics Nav 2015 oData 服务仅返回一条记录
- javascript - 如何规范罗盘输入?
- python - 在 Python 中从数据库中读取对象属性
- android - 以编程方式更改浮动按钮图像大小
- c++ - C++中定义语句的含义
- javascript - Tooltip 和 Popover 不是函数
- java - 如何使用令牌和正文从 android 应用程序向服务器提交数据?
- html - CSS超链接按钮在android Firefox中不起作用?
- php - scandir/opendir 不能与 ssh2.sftp 一起使用?
- php - 在 OceanWP 主题的 WooCommerce 中删除主商店的简短产品描述