1

I'm trying to run a for loop in a website table with Selenium. I'm using Xpath to get the data.

When I run this solo, I get the proper output:

driver.find_element_by_xpath('//*[@id="top-team-stats-summary-content"]/tr[1]/td[2]').text

But when I put in a for loop to extract all values, it runs an error

vals = (list(np.arange(0,20,1)))
for i in vals :
    print(driver.find_element_by_xpath('//*[@id="top-team-stats-summary-content"]/tr[i]/td[2]').text)

Error:

NoSuchElementException: Message: no such element: Unable to locate element:
 {"method":"xpath","selector":"//*[@id="top-team-stats-summary-content"]/tr[i]/td[2]"}

This is the HTML

<td class="goal   ">59</td>

Any thoughts?

EDIT: Before coming to a post, I looked at this post, that helped me a lot.

2 Answers2

1

You are very close mate. You simply need to concatonate your variable i within the string.

vals = (list(np.arange(0,20,1)))
for i in vals :
    print(driver.find_element_by_xpath('//*[@id="top-team-stats-summary-content"]/tr['+i+']/td[2]').text)
groeterud
  • 117
  • 7
  • Hey! Thanks for the help! unfortunetly I got this error: `TypeError: can only concatenate str (not "numpy.int32") to str` – Rodrigo Brust Apr 14 '21 at 21:51
  • right.. you have to convert it to a string within the forloop. Anyway, saw that the above answer worked for you, so that's great :) – groeterud Apr 15 '21 at 09:36
1

You have to pass the i parameter into the xpath expression.

xpath = '//*[@id="top-team-stats-summary-content"]/tr[{}]/td[2]'.format(i)
print(driver.find_element_by_xpath(xpath).text)

or simply concatenate the i value as suggested by groeterud

Prophet
  • 32,350
  • 22
  • 54
  • 79