找到包含未知范围文本的webelement

时间:2015-11-08 13:46:31

标签: java html selenium xpath

我在这里有这个html片段。

enter image description here

这是我想要实现的,在用户输入值之后,使用输入值来检查哪个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"
    }
} 

问题如何查找包含用户输入“text”而不是特定值的页面PagePostsSectionPagelet?​​

尝试了不同的方式,但都有 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()]"));

我的代码

    //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;
        }
    }

1 个答案:

答案 0 :(得分:0)

这是我从您的问题中理解的(如果我错了,请纠正我),您正在尝试搜索满足2个条件的特定div标记:

  1. id="PagePostsSectionPagelet"
  2. 并且在该标记内应该有一个span标记,该标记与用户输入的值相匹配(例如:2014)
  3. 现在,在div代码中,您想要使用div获取另一个class="_5sem"代码。

    如果我所解释的内容与您要实现的内容相符,那么下面给出的xpath应该适用于您上面提供的HTML源代码。

    "//span[text()='2014']/ancestor::div[contains(@id, 'PagePostsSectionPagelet')]//div[@class='_5sem']"
    

    您提供的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"