0

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)
3
  • 1
    Just a note, setting i equal to 1 right before the iteration does nothing. And that particular iteration, the way you have it set will begin at zero. Commented Dec 23, 2016 at 5:44
  • what output of ['%s+'] do you expect? Commented Dec 23, 2016 at 5:47
  • Sorry that was a typo Commented Dec 23, 2016 at 5:50

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.