Miskaone
Miskaone

Reputation: 31

Python Selenium select xpath with variable

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 + "']" + '"'))) 

Upvotes: 1

Views: 1627

Answers (1)

Miskaone
Miskaone

Reputation: 31

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 + "'" + ")]")

Upvotes: 2

Related Questions