简体   繁体   English

Python Selenium 选择带有变量的 xpath

[英]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:如果将以下内容与静态 xpath 搜索字符串一起使用,我就成功了:

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: Default-Test 将任意更改,我知道如何获取此值,但在 xpath 搜索中使用变量没有成功:

Test method 1 Does not work测试方法 1 不起作用

dtg_found = "Default-Test" 

Test method 2 does not work, this is the actual method for locating the value测试方法2不起作用,这是定位值的实际方法

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

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

I am facing with the same issue, but the above answer has given excluding "EC.visibility_of_element_located".. If anyone has overcome with generic xpath solution related to this query it would be great help.我面临着同样的问题,但上面的答案已经排除了“EC.visibility_of_element_located”。如果有人克服了与此查询相关的通用 xpath 解决方案,那将是很大的帮助。

Thanks in advance :)提前致谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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