首页 > 解决方案 > 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.

标签: pythonseleniumselenium-webdriverxpath

解决方案


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)

推荐阅读