Reputation: 27
In selenium, in python, I have to loop through Jira1, jira2, jira3 links
for i in range(1,4):
driver.find_element(By.XPATH,"//a[text()='Jira']+/str(i)").click()
It is giving me an error NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[text()='Jira+str(i)']"} (Session info: chrome=91.0.4472.77)
How do I resolve that
Upvotes: 0
Views: 164
Reputation: 4869
To use a variable in building the XPath you should correctly insert that variable like @Martheen is commenting.
So in stead of:
driver.find_element(By.XPATH,"//a[text()='Jira']+/str(i)").click()
Use this:
driver.find_element(By.XPATH,"//a[text()='Jira" + i +"']").click()
this will build i.e. the XPath: //a[text()='Jira1']
To have better controle over your loop and to know what is going on your loop, you could first build the XPath string and then use it, i.e. like this:
for i in range(1,4):
XPathInLoop = "//a[text()='Jira" + i + "']"
print(XPathInLoop)
driver.find_element(By.XPATH,XPathInLoop).click()
Upvotes: 0