为什么我的XPATH定位器突然停止工作?

时间:2017-03-03 09:37:18

标签: java selenium selenium-webdriver webdriver

  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元素: enter image description here

2 个答案:

答案 0 :(得分:1)

你可以找到最不可能长期改变的链接文本

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

如果您案例中的链接文字“驾驶”包含前导或尾随空格,则使用案例2.

答案 1 :(得分:0)

尝试类似......

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

干杯,