Hemapriya Viswanath
Hemapriya Viswanath

Reputation: 25

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.

Upvotes: 1

Views: 220

Answers (1)

Prophet
Prophet

Reputation: 33361

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)

Upvotes: 1

Related Questions