1
#driver.find_element_by_xpath('//div[@id="groups"]/div/div/div/div/div[2]/div[1]/div/div/button').click()
i = 5
gPath1 = str("\'//div[@id=\"groups\"]/div/div/div/div/div[2]/div[")
gPath2 = str(i)
gPath3 = str("]/div/div/button\'")
gPath = gPath1 + gPath2 + gPath3

driver.find_element_by_xpath(gPath).click()

I'm getting InvalidSelectorException error while using variable, but when i use the commented line it is working fine. Please help.

2

Remove \' from the beginning and from the end. So that your code would look like:

gPath1 = str("//div[@id=\"groups\"]/div/div/div/div/div[2]/div[")
gPath2 = str(i)
gPath3 = str("]/div/div/button")
gPath = gPath1 + gPath2 + gPath3

driver.find_element_by_xpath(gPath).click()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.