Reputation: 25
Trying to iterate though a list of elements by the index of a class. Each attempt the index doesnt increase. Adding quotes around %s produces an invalid syntax error.
lengeItem = len(driver.find_elements_by_xpath('//td[@class="baobab"]'))
i=1
for i in range(lengeItem):
Domaino = driver.find_element_by_xpath("//td[@class='baobab']['%s']/p/a" % i).text
print (Domaino)
print (Domaino)
Upvotes: 0
Views: 434
Reputation: 81
Answer: find_element_by_xpath("//td[@class='baobab'][%s]/p/a" % i).text
If i is 1, then %s
is '1'
. Since you are trying index using string td[@class='baobab']['1']
which throws error. It should be td[@class='baobab'][1]
Upvotes: 0
Reputation: 435
i is an integer, you need to use %d, not %s and don't wrap it in quotes, this should work: [%d]
els = driver.find_elements_by_xpath('//td[@class="baobab"]')
for i, el in enumerate(els):
print driver.find_element_by_xpath("//td[@class='baobab'][%d]/p/a" % (i + 1)).text
Upvotes: 1