Selenium 无法使用 xpath 定位元素,但 firebug 可以 [英] Selenium unable to locate element using xpath but firebug can

查看:60
本文介绍了Selenium 无法使用 xpath 定位元素,但 firebug 可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code to locate an element using a Xpath which using Firebug it works great. When I run my program I get the following exception:

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

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"(//div[@class=\" x-ignore x-menu x-component \"]//div)/a[text()=\"ID\"]"}

如果我采用那个确切的 xpath 并坚持使用 Firebug,我可以找到我的元素没问题.任何想法为什么 Selenium 找不到它?

If I take that exact xpath and stick in Firebug I can find my element no problem. Any ideas why Selenium can't find it?

这是我的代码:

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 之间的细微差别:

As mentioned in the comments, the slight difference between these two XPaths:

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 + "\"]";

除了最后的部分,后者在component"之后有一个空格,而前者没有.

besides the part at the end, is that the latter has a space after "component" and the former doesn't.

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

I suspect that the using normalize-space() and removing the leading and trailing spaces in the comparison values might help iron out inconsistencies in the spacing of the @class attribute value:

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 + "\"]";

这篇关于Selenium 无法使用 xpath 定位元素,但 firebug 可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆