TransWikia.com

Variable in Xpath is not working

Software Quality Assurance & Testing Asked by user46038 on October 25, 2021

#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.

One Answer

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

Answered by Alexey R. on October 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP