简体   繁体   中英

Python Selenium xpath with variable

I can't pass in a variable my search for xpath with selenium. What is my mistake?

btn_login = "'.//a[contains(text(), 'Login to')]'"

btn = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, f'{btn_login}')))​

btn.click()

like that it works, I don't know if it's possible to use an f string in this case.

btn = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, .//a[contains(text(), 'Login to')]')))

btn.click()

If you want the "Login to" text to come from a variable, do it this way:

text = "whatever"
btn_login = f".//a[contains(text(), '{text}')]"

btn = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, btn_login)))​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM