0

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
asked Jan 2, 2023 at 18:51
1
  • That xpath string literally contains the outermost single-quotes, which I don't think you intended. Commented Jan 2, 2023 at 18:59

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.