简体   繁体   English

By.xpath(contains(text(),“ Some text”)给错误

[英]By.xpath(contains(text(), “Some text”) gives error

The following code says that "The method text() is undefined for the type Test" and prompts me to create a new function text() in class Test. 下面的代码说“类型Test的方法text()未定义”,并提示我在Test类中创建一个新的函数text()。

driver.findElement(By.xpath(contains(text(), "menu")));

I am using Eclipse Kepler and Selenium 2.39.0. 我正在使用Eclipse Kepler和Selenium 2.39.0。

The exception i receive is : org.openqa.selenium.NoSuchElementException . 我收到的异常是: org.openqa.selenium.NoSuchElementException I am not able to figure out where am i going wrong. 我无法弄清楚我要去哪里错了。

XPath expressions need to be surrounded by quotes - and since the expression you are trying to parse also contains a string literal, I would suggest you switch the literal to single apostrophe ' . XPath表达式需要用引号引起来-并且由于您要解析的表达式还包含字符串文字,因此建议您将文字切换为单撇号' Also, unless you expect the root element to contain the text menu , you'll need to be more specific about the element you are searching for. 另外,除非您希望根元素包含文本menu ,否则您将需要更详细地了解要搜索的元素。 For example, the following xpath: 例如,以下xpath:

 driver.findElement(By.xpath("//*[contains(text(), 'menu')]"));

Will find the li element with the text "menu" (note xpath is case-sensitive): 将找到带有文本“ menu”的li元素(注意xpath区分大小写):

<html>
    <foo>
        <bar>
            <ul>
                <li id="123">menu</li>
            </ul>
        </bar>
    </foo>
</html>

If possible, be even more certain, eg if you know that it is a li element: 如果可能,请更加确定,例如,如果您知道它是li元素:

 driver.findElement(By.xpath("//li[contains(text(), 'menu')]"));

Edit (OP put the actual html up) 编辑 (OP放上实际的 html)

This will find the DIV element: 这将找到DIV元素:

 driver.findElement(By.xpath("//DIV[@style[contains(., 'm‌​enubar_menubutton.png')]]"));

Note that xpath is case sensitive - so you'll need to duplicate the SHOUTCASE tags. 请注意,xpath区分大小写-因此,您需要复制SHOUTCASE标记。

i feel there is a much easier way to do this. 我觉得有一种更简单的方法可以做到这一点。

i'd personally just do: 我个人只是做:

WebElement we = driver.findElement(By.Id("123"))

or if you'd like to leverage css selectors you could do: 或者,如果您想利用CSS选择器,可以执行以下操作:

WebElement we = driver.findElement(By.cssSelector("li:nth-child(1)"))  //baring you know the index of the list

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

相关问题 如何在 findElement(By.xpath(&quot;//span[contains(text(),&#39;string&#39;)]&quot;)) 中使用表达式 - How to use an expression in a findElement(By.xpath("//span[contains(text(),'string')]")) 我想在运行时更改元素的包含文本 - By.xpath("//a[contains(text(),'ProductName')]"); - I want to change the contains text of the element on run time - By.xpath("//a[contains(text(),'ProductName')]"); 硒问题尝试使用 driver.findElement(By.xpath() 从 div 中检索文本 - selenium issue trying to retrieve text from a div using driver.findElement(By.xpath() xpath contains(text(),'some string' 与数组元素一起使用时不起作用 - xpath contains(text(),'some string' doesn't work when used with element of an array xPath 文本包含 Selenium Web 驱动程序 - xPath Text Contains Selenium Web Driver 具有ID且包含文本的Xpath表达式 - Xpath expression with both an ID and contains Text Xpath 包含文本()不适用于注释 - Xpath contains text() doesnt work with comments Java的 远程服务器返回503错误,页面包含一些文本。 如何获得此文字? - java. Remote server returns 503 error with page, that contains some text. How Get this text? 在WebDriver By.XPATH中使用通配符 - Using wildcard in WebDriver By.XPATH 如何将`regex`嵌入到`By.xpath ...`中? - How to embed `regex` into the `By.xpath…`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM