2017-03-03 112 views
0
  1. 0-415构建过去了
  2. 416-550失败为什么我的XPATH定位器突然停止工作?

  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

+1

什么意思是“停止工作”?它现在是否给出'NoSuchElementException'或者不会触发任何事件?我尽量避免使用XPath,而是使用CssSelector。通常情况下,您可以通过更简短的方式找到元素,DOM中的更改不会因为使用CssSelector而轻松破坏测试。 – Tom

+1

考虑到提供'HTML','XPath'似乎没问题。共享异常日志 – Andersson

+0

请将HTML作为文本而不是图像发布。它使潜在的答复者更容易使用和阅读。你尝试过一些简单的东西,比如'By.linkText(“Driving”)''或'By.cssSelector(“a [href ='/ driving-experiences']”)'?您可能会看到嵌套过深的XPath的影响。没有HTML之前和之后很难说,等等。 – JeffC

回答

0

试着像...

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

干杯,

1

你可以去其中大多数可能不会从长远来看

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

使用案例2改变链接文本,如果链接文本在您的案件“驾驶”包含前导或拖尾的白色空间。