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 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It just solved my problem. It was a silly one, how could I miss that. I appreciate!
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)

2 Comments

Hey! Thanks for the help! unfortunetly I got this error: TypeError: can only concatenate str (not "numpy.int32") to str
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 :)

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.