【发布时间】:2016-02-09 05:50:11
【问题描述】:
我这里有这个 html sn-p。
这就是我想要实现的,用户输入一个值后,使用输入值检查哪个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"
}
}
问题如何找到包含用户输入“文本”而不是特定值的webelements PagePostsSectionPagelet?
尝试了不同的方法,但都有这个 SyntaxError: The expression is not a legal expression。
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()]"));
我的代码
//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;
}
}
【问题讨论】: