【问题标题】:Why has my XPATH locator suddenly stopped working?为什么我的 XPATH 定位器突然停止工作?
【发布时间】:2017-03-03 18:26:02
【问题描述】:
  1. 0-415 构建已通过
  2. 416-550 失败了

  3. 以下定位器用于工作:

    public @FindBy(xpath = ".//*[@id='menu']/ul//a[@href='/driving-experiences']") WebElement link_DrivingExperiences;
    
  4. 现在它突然停止工作,我尝试使用绝对 XPATH 并且有效;我试图避免,我打算使用动态定位器。

  5. 我的点击方式:

    public void waitAndClickElement(WebElement element) throws InterruptedException {
    boolean clicked = false;
    int attempts = 0;
    while (!clicked && attempts < 3) {
        try {
            this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
            System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
            clicked = true;
        } catch (Exception e) {
            System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
            Assert.fail("Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
        }
        attempts++;
    }
    

    }

  6. HTML 元素:

【问题讨论】:

  • 什么是“停止工作”?它现在给NoSuchElementException 还是不触发任何事件?我尽量避免使用 XPath 并改用 CssSelector。通常,您可以以更短的方式找到元素,DOM 中的更改不会使用 CssSelector 轻易破坏您的测试。
  • 考虑到提供HTML 你的XPath 似乎没问题。分享异常日志
  • 请将 HTML 发布为文本而不是图像。它使潜在的回答者更容易使用和阅读。你试过像By.linkText("Driving")By.cssSelector("a[href='/driving-experiences']") 这样简单的东西吗?您可能会看到嵌套太深的 XPath 的影响。没有前后HTML等就很难说了。

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

您可以选择从长远来看很可能不会更改的链接文本

//a[text()='Driving'] or
//a[contains(text(),'Driving')]

如果您的案例中的链接文本“Driving”包含前导或尾随空格,则使用案例 2。

【讨论】:

    【解决方案2】:

    尝试类似...

    public @FindBy(xpath = "//a[contains(@class, 'toplevellink') and contains(@href, 'driving-experiences')]"))

    干杯,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 2019-02-26
      • 2019-10-15
      • 1970-01-01
      相关资源
      最近更新 更多