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.

1 Answer 1

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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