2015-10-19 43 views
2

我知道有關於這個主題的幾個主題,但我不一定在尋找解決方案,而是一個解釋。我在一個非常大的自動化套件上工作,該套件使用瀏覽器通過手機測試Web應用程序。我的穩定性很低..這是由於這個錯誤引發了我!偶爾它會工作,偶爾它不會..我不能使用操作,因爲Browserstack不支持該..爲什麼這個錯誤存在,並有任何人有任何成功解決它。我總是在等待一個使用wait.until(ExpectedConditions)的對象,但有時候這樣做效果不好。因爲它是一個未知的錯誤,所以我不能把它作爲例外來捕捉。另外,我們的標準不允許使用Thread.sleep()。有任何想法嗎?謝謝你這麼多Selenium WebDriver - 元素不可點擊,爲什麼?

enter image description here

這裏是一些代碼的屏幕.. enter image description here

+0

你可以張貼一些故障跟蹤我們調用這個函數 public static void waitForElementPresent(final By by, int timeout,WebDriver driver) 在此之後?或者任何代碼? – jagdpanzer

+0

這也可能是一個有爭議的問題,但是你需要使用BrowserStack嗎? – jagdpanzer

+0

@jagdpanzer,我更新了這篇文章給你一個例子。目前這是我們擁有的最佳解決方案。我們現在正在使用硒電池。 – Tree55Topz

回答

1

元素通常不能夠點擊,由於以下原因。

  1. HTML是加載和客戶端仍然從服務器
  2. 由於一些對象重複的對象

問題3接收更新滾動時

  • 它可以是解決不了的,你需要修正你的代碼在這等待我等待HTML準備,然後驗證它是否可以點擊或不能這已經消除了從我的代碼這樣的例外

    我怎麼做了解決方案的概率lem 1和2,您可以簡單地使用我的自定義等待,然後單擊。如果你正在使用其他的瀏覽器之後,Chrome,然後調用滾動到對象,這將解決你的問題 代碼

    public static void waitForElementPresent(final By by, int timeout,WebDriver driver) { 
    
         waitForPageLoad(driver); 
         WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,40).ignoring(StaleElementReferenceException.class); 
         /* wait.until(new ExpectedCondition<Boolean>(){ 
          @Override 
          public Boolean apply(WebDriver webDriver) { 
           WebElement element = webDriver.findElement(by); 
           return element != null && element.isDisplayed(); 
          } 
          }); */ 
          try { 
          Thread.sleep(1000); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
          wait.until(ExpectedConditions.presenceOfElementLocated(by)); 
          wait.until(ExpectedConditions.elementToBeClickable(by)); 
          WebDriverWait wait2 = new WebDriverWait(driver, 40); 
          wait2.until(ExpectedConditions.elementToBeClickable(by)); 
    
         } 
        //wait for page to laod 
        public static void waitForPageLoad(WebDriver driver) { 
         ExpectedCondition<Boolean> pageLoadCondition = new 
          ExpectedCondition<Boolean>() { 
           public Boolean apply(WebDriver driver) { 
            return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"); 
           } 
          }; 
         WebDriverWait wait = new WebDriverWait(driver, 30); 
         wait.until(pageLoadCondition); 
        } 
    
  • 0

    這是由於的速度,硒運行。它會在頁面加載之前嘗試查找元素,從而導致出現此錯誤。

    2

    您正在等待WebElement可點擊,然後再次查找WebElements列表並單擊第一個元素。 這並不能保證你點擊你等待點擊的元素。

    public void waitAndClickElement(WebElement element) { 
        driverWait.until(ExpectedConditions.elementToBeClickable(element)).click(); 
    } 
    

    在你的情況,

    public void clickImageView() { 
        driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click() ; 
    } 
    
    +0

    我會嘗試這一點,因爲它似乎使很多感覺!謝謝! – Tree55Topz

    0

    對於您所提供的代碼示例中,.until()返回WebElement你正在等待。你可以使用下面的代碼來點擊它,而不是再次抓取頁面。

    public void clickImageView() 
    { 
        driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click(); 
    } 
    
    相關問題