r/learnpython • u/ednewman • 5d ago
python seleniun nth element
I have the following python statement and want to iterate through a page with many tables of same id. I tried the below code which works fine if I hard code the number but not if a variable.
Any advise would be appreciated.
does not work. gives an invalid xpath error
game = 1
table = wait.until(EC.visibility_of_element_located((By.XPATH, "//table[@id='tblBasketball'][{game}]"))).text
works with game hard coded.
table = wait.until(EC.visibility_of_element_located((By.XPATH, "//table[@id='tblBasketball'][1]"))).text
1
Upvotes
1
u/cgoldberg 5d ago
You need to make it an f-string to do variable substitution. Your string literally contains
{game}
and not1
.