代码之家  ›  专栏  ›  技术社区  ›  Ishita Shah Y_Sh

Xpath con cat可以引用selenium中的List<WebElement>对象吗?

  •  0
  • Ishita Shah Y_Sh  · 技术社区  · 7 年前

    • 表中的10个不同行(例如,//a[@class='*****'])。
    • 需要从每一行检索不同的值
    • 使用列表检索
    • 需要检索依赖于对象的Temp2值 查找列表

    问题是:

    • 如果我执行第二个provision,使用con cat的xpath+List对象会抛出xpath语法问题。

    问题: 如何从对象的同级对象中检索所有信息 List<WebElement> findList

    试验:

    By mainList = By.xpath("//a[@class='*******']");
    By findSubInfo = By.xpath("//a[@class='*****']//following::div[@class='****']");
    
        List<WebElement> findList = driver.findElements(mainList);
    
            for (WebElement webElement : findList ) {
                if (webElement.isDisplayed()) {
    
                String temp1= "Info1:" + webElement.getAttribute("ng-href");                    
                String temp2= "Info2: " + driver.findElement(findSubInfo).getText();
                }
            }
    

    String mainList "//a[@class='*******']";
    String findSubInfo = "//following::div[@class='****']";
    
         List<WebElement> findList = driver.findElements(By.xpath(mainList));
    
            for (WebElement webElement : findList ) {
                if (webElement.isDisplayed()) {
    
                String temp1= "Info1:" + webElement.getAttribute("ng-href");    
                String temp2= "Info2: " + driver.findElement(By.xpath(webElement + findSubInfo )).getText();
                }
            }
    

    异常详细信息:

    '[[铬驱动程序:XP上的铬(3208ef0a2ffd3212c33e159291eebe4)]-> addPointer']//following::div[@class='assignedAccountMasterAddress']'

    1 回复  |  直到 7 年前
        1
  •  1
  •   Andersson    7 年前

    如果要从当前元素开始搜索具有相关XPath的元素( webelement

    # Note that it should start with the dot
    String findSubInfo = "./following::div[@class='****']";
    # use webElement.findElement instead of driver.findElement. No XPath concatenations needed
    String temp2= "Info2: " + webElement.findElement(By.xpath(findSubInfo)).getText();