Rodrigo Brust
Rodrigo Brust

Reputation: 70

How to get text with Selenium WebDriver in a For Loop Python

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.

Upvotes: 1

Views: 1071

Answers (2)

Prophet
Prophet

Reputation: 33361

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

Upvotes: 1

groeterud
groeterud

Reputation: 127

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)

Upvotes: 1

Related Questions