1

I am trying to locate a visible element that will change based on what the user enters on the website. I am successful if use the follow with a static xpath search string:

wait.until(EC.visibility_of_element_located((By.XPATH, "//a[text()='Default-Test']")))

Default-Test will change arbitrarily and i have how to get this value but I have not been successful using a variable in the xpath search:

Test method 1 Does not work

dtg_found = "Default-Test" 

Test method 2 does not work, this is the actual method for locating the value

dtg_found = driver.find_element_by_name("result[0].col[1].stringVal").get_attribute("value")

dtg_opt_1 = wait.until(EC.visibility_of_element_located((By.XPATH, "\"//a[text()='" + dtg_found + "']" + '"'))) 
5
  • How many quotation marks do you have in there? Commented Jul 6, 2015 at 22:22
  • Thanks, I tried changing up the escapes "\" and tried concatenating the string together without "\". Finally got it work with the following: dtg_opt_1 = driver.find_element_by_xpath("//*[contains(text()," + " '" + dtg_found + "'" + ")]") Commented Jul 6, 2015 at 23:55
  • If you've resolved the question, then you should either delete it, or provide an answer below (and accept it) so that the question won't clutter up the unanswered questions page. Commented Jul 7, 2015 at 18:04
  • What gave you the impression that you should have a \" at the beginning of the XPath expression? Commented Jul 7, 2015 at 18:06
  • @LarsH I am a bit new to python but I was trying to concatenate the xpath search string with variable. Because the search string as mixed quotes I thought I could use \ to escape the following quote. I was clearly wrong. Commented Jul 9, 2015 at 23:03

1 Answer 1

2

This is the solution to the issue I was encountering. Tried reworking the string and the related escapes '\' but was not successful. Did however get the following working. I am not clear why this worked.

dtg_opt_1 = driver.find_element_by_xpath("//*[contains(text()," + " '" + dtg_found + "'" + ")]")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.