简体   繁体   English

Selenium Java-list元素在无序列表中包含部分文本

[英]Selenium Java - list element contains partial text within unordered list

How can I add 'starts-with' or 'contains' for 我如何为以下项目添加“开始于”或“包含”

(By.xpath("//ul/li[1]")));

something like 就像是

(By.xpath("//ul/li[1][contains(text(),'"+textConstant+"')]")));  

which doesn't work 这不起作用

Can anyone assist with the proper formatting for this? 任何人都可以协助正确格式化吗? Thanks 谢谢

If Only one li tag contains that text then no need to mention index. 如果只有一个li标签包含该文本,则无需提及索引。

You can write like this: 您可以这样写:

By locator = By.xpath("//ul/li[contain‌​s(., '"+lostText+"')]");
WebElement errorFind = (new WebDriverWait(mypackage.ClassMain.driver, 15)).until(ExpectedConditions.presenceOfElementLocated(locator));
String text = mypackage.ClassMain.driver.findElement(By.xpath("//ul/li[1]")).getText(); 
if(text.equals("Lose") {
   doNext(); 
} else {
   System.out.print("Win \n"); 
   doOther();
}

If textConstant is coming inside li tag, then you can write in following manner:- 如果textConstant进入li标记内,则可以按以下方式编写:

(By.xpath("//ul/li[contains(.,'"+textConstant+"')]));

here, dot . 在这里,点。 stands for text() 代表text()

You can use the AND operator for the Xpath. 您可以对Xpath使用AND运算符。

//ul/li[position()=1 and contains(text(),'"+textConstant+"')]

This will match the top most li tagwhich contains the text constant value. 这将匹配包含文本常量值的最上面的li标签。

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

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