【问题标题】:find webelement that contains unknown span text查找包含未知跨度文本的 web 元素
【发布时间】: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;
        }
    }

【问题讨论】:

    标签: java html selenium xpath


    【解决方案1】:

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

    1. 有一个id="PagePostsSectionPagelet"
    2. 并且在该标签内应该有一个span标签与用户输入的值相匹配(例如:2014)

    现在,在该 div 标记中,您想获取另一个带有 class="_5sem"div 标记。

    如果我解释的内容与您想要实现的内容相匹配,那么下面给出的 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"
    

    【讨论】:

    • 是的,您正确理解了我的问题。但是,有一个 NoSuchElementException: Unable to locate element:{"method":"xpath","selector":"//div[@id='PagePostsSectionPaglet' and span[text()='2013'] ]/div[@id='_5sem']"} 当我通过使用扫描仪输入 2013 年的用户输入时。 @杰森
    • 即使我将您的代码更改为/div[@class='_5sem'],它仍然有相同的NoSuchElementException错误@jason
    • @dark_space - 你能分享 HTML 代码或链接吗?使用 gist.github.com 分享源码。
    • @dark_space - 或者把&lt;div id='PagePostsSectionPaglet'&gt;的内容发给我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    相关资源
    最近更新 更多