2017-03-03 71 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改變鏈接文本,如果鏈接文本在您的案件「駕駛」包含前導或拖尾的白色空間。