简体   繁体   English

如何使用%s在python中为Selenium构造带有变量的xpath表达式

[英]How to construct a xpath expression with variable for Selenium in python using %s

I am trying to use selenium to locate and select a certain element based on a variable passed into the function when it is called. 我正在尝试使用硒根据调用时传递给函数的变量来定位和选择某个元素。 I thought this would be simple enough with a good ol: 我以为,有了一个好的ol,这将足够简单:

show = browser.find_element_by_xpath("//*[contains(text(), '%s')]" % eventName)

However, I receive error: 但是,我收到错误消息:

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[contains(text(), 'VARIABLE_TEXT')] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(text(), 'VARIABLE_TEXT')]' is not a valid XPath expression.

Here is--distilled and simplified--the part of the HTML I am attempting to select: 这是我试图选择的HTML的一部分-提炼和简化了:

<ul>
<li class="seriesElement">
<input id="productionId_0" name="productionIds" type="checkbox" value="VALUE"><input type="hidden" name="_productionIds" value="on"><label for="productionId_0" class="prodColor0">TEXT</label>
</li>
<li class="seriesElement">
<input id="productionId_1" name="productionIds" type="checkbox" value="VALUE"><input type="hidden" name="_productionIds" value="on"><label for="productionId_1" class="prodColor1">TEXT</label>
</li>
</ul>

There are many of these list elements, and I want to select one by the text. 这些列表元素很多,我想按文本选择一个。 Have I just used the wrong syntax for my xpath expression, or is this a different beast entirely? 我是否对xpath表达式使用了错误的语法,或者这完全是另一种野兽?

You can try this way.See if this helps. 您可以尝试这种方式。看看是否有帮助。

VAR_TEXT="TEXT"
print(driver.find_element_by_xpath("//label[contains(text() ,'" + VAR_TEXT + "')]").text)

OR 要么

VAR_TEXT="TEXT"
print(driver.find_element_by_xpath("//*[contains(text() ,'" + VAR_TEXT + "')]").text)

OR 要么

VAR_TEXT="TEXT"
print(driver.find_element_by_xpath("//label[text()[contains(.,'" + VAR_TEXT + "')]]").text)

OR 要么

VAR_TEXT="TEXT"
print(driver.find_element_by_xpath("//*[text()[contains(.,'" + VAR_TEXT + "')]]").text)

To locate a certain element based on it's variable text you can write a method as follows: 要基于可变文本定位某个元素,可以编写如下方法:

  • Using . 使用. :

     def find_myelement_with_text(myString): show = browser.find_element_by_xpath("//label[.='" + myString + "']") 
  • Using %s : 使用%s

     def find_myelement_with_text(myString): show = browser.find_element_by_xpath("//label[contains(text(), '%s')]" % (myString)) 
  • Using format() : 使用format()

     def find_myelement_with_text(myString): show = browser.find_element_by_xpath('//label[contains(text(), "{}")]'.format(myString)) 

Now you can call this method from anywhere within your program as follows: 现在,您可以在程序中的任何位置调用此方法,如下所示:

find_myelement_with_text("TEXT")

The good ol' 😀 %s worked as expected - you can see the substitution in the error message, the problem is with the xpath itself. 好的%s工作-您可以在错误消息中看到替换,问题出在xpath本身。
If you run it through a standalone parser (not the browser's one), you'll see the culprit for the error: 如果通过独立的解析器(而不是浏览器的解析器)运行它,则会看到错误的元凶:

Unable to perform XPath operation. 无法执行XPath操作。 A sequence of more than one item is not allowed as the first argument of contains() ("", "", ...) 包含一个以上项目的序列不允许作为contains()(“”,“”,...)的第一个参数

(that's a Java one, the text will be similar for other ones) (这是Java语言,其他语言则类似)

Long story short it's the output of text() that bothers it - it's a nodeset; 长话短说,困扰它的是text()的输出-它是一个节点集; if you change that function to a . 如果您将该功能更改为. , it should work: ,它应该可以工作:

show = browser.find_element_by_xpath("//*[contains(., '%s')]" % eventName)

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

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