【问题标题】:Selenium unable to locate element using xpath but firebug canSelenium 无法使用 xpath 定位元素,但 firebug 可以
【发布时间】:2013-01-18 15:29:47
【问题描述】:

我有以下代码使用 Xpath 定位元素,使用 Firebug 效果很好。当我运行我的程序时,出现以下异常:

线程 "main" org.openqa.selenium.NoSuchElementException 中的异常:无法定位元素:{"method":"xpath","selector":"(//div[@class=\" x-ignore x -menu x-component \"]//div)/a[text()=\"ID\"]"}

如果我采用确切的 xpath 并坚持使用 Firebug,我会发现我的元素没有问题。任何想法为什么 Selenium 找不到它?

这是我的代码:

public static void displayColumn(String column) throws Exception {
    String columnOptionsDropdownXpath = "(//div[@class=\"x-grid3-header\"]//span)[1]/../a";
    String columnXpath = "(//div[@class=\"x-grid3-header\"]//span)[1]";
    String columnsXpath = "(//div[@class=\" x-ignore x-menu x-component\"]//a)[3]";
    String columnToDisplayXpath = "(//div[@class=\" x-ignore x-menu x-component \"]//div)/a[text()=\"" + column + "\"]";

    // Because the 'column options' button doesn't appear until you hover over the column
    WebElement col = null;
    try {
        col = driver.findElement(By.xpath(columnXpath));
    } catch (NoSuchElementException e) {
        System.out.println("Column not found - is it displayed?");
    }

    Actions builder = new Actions(driver);
    builder.moveToElement(col).build().perform();
    WebElement element = driver.findElement(By.xpath(columnOptionsDropdownXpath));
    element.click();
    Thread.sleep(500);

    element = driver.findElement(By.xpath(columnsXpath));
    builder.moveToElement(element).build().perform();
    Thread.sleep(2000);
    WebDriverWait wait = new WebDriverWait(driver, 10);
    try {
        System.out.println("in try statement");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(columnToDisplayXpath)));
    } catch (TimeoutException e) {}

    element = driver.findElement(By.xpath(columnToDisplayXpath));
    element.click();
}

【问题讨论】:

  • 如果在 XPath 中将 text() 替换为 .normalize-space(.) 怎么样?
  • @JLRishe - 我试过了,但没有区别
  • 你确定元素在运行时已经完全加载了吗?尝试在 columnsXpath 元素上调用 findElement 之前插入等待。
  • 看起来columnToDisplayXpath 的XPath 在component 之后有一个空格,但columnsXPath 的XPath 没有。您可以尝试删除该空间吗?
  • @JLRishe 就是这样!奇怪的是,如果我在 Xpath 萤火虫中删除了该空间,则无法找到该元素,反之亦然,因此在我的代码中删除它就像在黑暗中射击。非常感谢!

标签: java xpath selenium selenium-webdriver


【解决方案1】:

正如 cmets 中提到的,这两个 XPath 之间的细微差别:

String columnsXpath = "(//div[@class=\" x-ignore x-menu x-component\"]//a)[3]";
String columnToDisplayXpath = "(//div[@class=\" x-ignore x-menu x-component \"]//div)/a[text()=\"" + column + "\"]";

除了最后的部分,就是后者在“组件”之后有一个空格,而前者没有。

我怀疑使用 normalize-space() 并删除比较值中的前导和尾随空格可能有助于消除 @class 属性值的间距不一致:

String columnsXpath = "(//div[normalize-space(@class) = \"x-ignore x-menu x-component\"]//a)[3]";
String columnToDisplayXpath = 
    "(//div[normalize-space(@class) = \"x-ignore x-menu x-component\"]//div)/a[text()=\""
    + column + "\"]";

【讨论】:

  • 我用 normalize-space() 进行了测试,它一直失败,直到我手动删除了空白,除非我做错了。下次遇到类似的事情时,我一定会记住这一点。再次感谢。
  • 嗯,在我上面的例子中,你可以看到我已经手动删除了 XPath 中 字符串值 的前导和尾随空格,而 normalize-space() 是在那里考虑实际属性值中的空格。如果将normalize-space() 放在@class 周围,并在XPath 中的“x-ignore x-menu x-component”前后留有空格,那肯定行不通。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
  • 2021-04-11
  • 2019-10-13
相关资源
最近更新 更多