[英]find webelement that contains unknown span text
I have this html snippet here. 我这里有这个html片段。
This is what I want to achieve, after a user input a value, use the input value to check which PagePostsSectionPaglet contains it, find the PagePostsSectionPaglet and find the className("_5sem")
that is in the same PagePostsSectionPaglet 这是我想要实现的,在用户输入值之后,使用输入值来检查哪个PagePostsSectionPaglet包含该值,找到PagePostsSectionPaglet并找到同一PagePostsSectionPaglet中的
className("_5sem")
get a list of text from webelement
//compare a list of text in PagePostsSectionPaglet with unknown user input
if text in PagePostsSectionPaglet = input year
{
find PagePostsSectionPaglet that contains text that is same with input year
go through same PagePostsSectionPaglet and find className="_5sem">
{
find UFIPagerLink //inside class="_5sem"
find UFIPagerIcon //inside class="_5sem"
}
}
Problem how to findwebelements PagePostsSectionPagelet that consists user input "text" instead of specific value? 问题如何找到包含用户输入“ text”而不是特定值的webelements PagePostsSectionPagelet?
tried different ways but all have this SyntaxError: The expression is not a legal expression. 尝试了不同的方法,但都具有此SyntaxError:该表达式不是合法表达式。
List <WebElement> PagePostsSectionPagelet = elm.findElements(By.xpath("//*[starts-with(@id, 'PagePostsSectionPagelet')]//contains('"+elm.getText()+"')"));
List <WebElement> PagePostsSectionPagelet = elm.findElements(By.xpath("//*[starts-with(@id, 'PagePostsSectionPagelet')]//span[elm.getText()]"));
my codes 我的密码
//tried to print the list of texts in AllPagePostsSectionPagelet but only returns the first value
List <WebElement> AllPagePostsSectionPagelet= dr.findElements(By.xpath("//*[starts-with(@id, 'PagePostsSectionPagelet')]//span[@class='_5em9 lfloat _ohe _50f4 _50f7']"));
for (WebElement elm : AllPagePostsSectionPagelet){
if(year.equals(elm.getText())){ //year is the user input value
//get weblement that contains the text
List <WebElement> PagePostsSectionPagelet = elm.findElements(By.xpath("//*[starts-with(@id, 'PagePostsSectionPagelet')]//elm.getText()")); //this is the issue
System.out.println(PagePostsSectionPagelet);
for (WebElement elment : PagePostsSectionPagelet){
List<WebElement> comments = elment.findElements(By.className("_5sem")); //find web element
//iterate through the comments
for (WebElement comment : comments) {
//find web elements by their respective class name
List<WebElement> commentsbutton = comment.findElements(By.className("UFIPagerLink"));
List<WebElement> repliesbutton = comment.findElements(By.className("UFIPagerIcon"));
}
}
//return;
}
}
This is what I understood from your question (Please correct me if I'm wrong), you are trying to search for a specific div
tag which satisfies 2 conditions: 这是我从您的问题中了解的信息(如果我错了,请纠正我),您正在尝试搜索满足以下两个条件的特定
div
标签:
id="PagePostsSectionPagelet"
id="PagePostsSectionPagelet"
span
tag which matches the value entered by the user (eg: 2014) span
标签(例如:2014) Now, within that div
tag, you want to fetch another div
tag with class="_5sem"
. 现在,在该
div
标签中,您要获取另一个带有class="_5sem"
div
标签。
If what I've explained matches with what you are trying to achieve, then the xpath given below should work for the HTML source you've provided above. 如果我所解释的与您要实现的目标相匹配,那么下面给出的xpath应该适用于您在上面提供的HTML源代码。
"//span[text()='2014']/ancestor::div[contains(@id, 'PagePostsSectionPagelet')]//div[@class='_5sem']"
The Java code which you've provided can be reduced to: 您提供的Java代码可以简化为:
//year is the user input value
String xp = "//span[text()='" + year + "']/ancestor::div[contains(@id, 'PagePostsSectionPagelet')]//div[@class='_5sem']";
WebElement comment = driver.findElement(By.xpath(xp));
// Add code to fetch 'UFIPagerLink' and 'UFIPagerIcon'
// which should be inside the div tag with class="_5sem"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.