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()
Prophet
33.5k29 gold badges58 silver badges90 bronze badges
-
That xpath string literally contains the outermost single-quotes, which I don't think you intended.John Gordon– John Gordon2023年01月02日 18:59:51 +00:00Commented Jan 2, 2023 at 18:59
1 Answer 1
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)))
answered Jan 2, 2023 at 19:02
John Gordon
33.8k9 gold badges49 silver badges72 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default