2016-09-30 45 views
0

以下是我的硒測試用例的代碼片段,其中我使用select方法從下拉列表中選擇一個值。接下來的步驟是點擊提交。但是,當我嘗試點擊提交按鈕頁面不刷新(這將刷新相同的頁面),投擲selenium web驅動等到頁面刷新

element not clickable,StaleElementReference exception

。唯一適用於我的解決方案是thread.sleep()

我嘗試了所有的以下選項,但沒有運氣:(

explicit wait()wait.until(Exceptedcontions.visibility)element to be clickable()等,試圖日網絡上的所有解決方案。

我不得不用在thread.sleep() 3-4次測試用例和我有大約100個測試用例花費了大量的時間

任何工作的解決方案,其中的網頁驅動程序等待,直到頁面被完全刷新,並在點擊提交按鈕之前DOM完全加載

@Test 
@Timeout(group = Group.SLOW) 
public void testProvider() throws InterruptedException { 

    proceedToProvider(); 
    new Select(driver.findElement(By.id("searchId"))).selectByVisibleText("Search"); 
    Thread.sleep(2000); 
    driver.findElement(By.id("btnSubmit")).click(); 
    timeSplit("Search submitted"); 

下面是我看到的錯誤,當我使用其他解決方案。

org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (1289, 141). Other element would receive the click: (Session info: chrome=53.0.2785.116) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 168 milliseconds Build info: version: '2.49.1', revision: '808c23b0963853d375cbe54b90bbd052e2528a54', time: '2016-01-21 09:37:52' System info: host: 'ALAKASIMA01-W7L', ip: '10.145.45.233', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_73' Driver info: org.openqa.selenium.remote.RemoteWebDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4), userDataDir=C:\Users\kasima01\AppData\Local\Temp\scoped_dir6628_12218}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=53.0.2785.116, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 8bf0b4cc7efc715015509f4be345d14d

回答

0
public void WaitForElementToLoad(String selector, String key, String seconds) 
      throws ValidationException { 

     Integer time = Integer.valueOf(seconds); 

     WebElement element = null; 

     while (time > 0) { 

      try { 
       element = getSpecificWebElement(selector, key); 

       break; 
      } catch (ValidationException e) { 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e1) { 
        e1.printStackTrace(); 
       } 
       time--; 
      } 
     } 

     if (element == null && time <= 0) { 
       // throw your own exception 
     } 
    } 

以上方法顯示的基本方法可以做到這一點。 getSpecificWebElement是一種方法,可以由您自己定義使用findElementByXX並拋出您自己的異常來捕獲。通過上限秒,你會等待特定的元素。

也有另一種解決方案 - 使用driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.

更新:

我想我最好後getSpecificWebElement

private WebElement getSpecificWebElement(String selector, String key) 
      throws ValidationException { 
     WebElement element = null; 

     String expression = ""; 

     By by = composeSelector(selector, key); 

     try { 
      element = FindElementBy(by); 
     } catch (TimeoutException e) { 

      throw new ValidationException("not found " + expression); 
     } catch (Exception e) { 
      throw new ValidationException(
        Exceptions.getShortStackTraceAsString(e)); 
     } 

     return element; 
} 


private WebElement FindElementBy(By by) { 

     final By selector = by; 

     ExpectedCondition<WebElement> ec = new ExpectedCondition<WebElement>() { 

      @Override 
      public WebElement apply(WebDriver driver) { 

       return driver.findElement(selector); 

      } 

     }; 

     WebDriverWait wait = new WebDriverWait(driver, 10); 

     return wait.until(ec); 
    } 

在這種情況下,等待時間就足夠了,將是X(通過秒數值)* 10。

+0

@Test \t @Timeout(組= Group.SLOW) \t公共無效testProviderSearchDataShareOn()拋出InterruptedException的,ValidationException { \t \t proceedToProviderScreen(); \t \t new Select(driver.findElement(By.id(「searchId」)))。selectByVisibleText(「Search」); // \t \t Thread.sleep(2000); \t \t WaitForElementToLoad(「id」,「btnSubmit」,「20」);相同的元素不可點擊的錯誤。 – maddie

+0

考慮一些元素覆蓋了你的目標元素。您需要移除屏障。 –

0

試試這個會檢查整個頁面是否加載?

static void waitForPageLoad(WebDriver wdriver) { 
    WebDriverWait wait = new WebDriverWait(wdriver, 60); 

    Predicate<WebDriver> pageLoaded = new Predicate<WebDriver>() { 

     @Override 
     public boolean apply(WebDriver input) { 
      return ((JavascriptExecutor) input).executeScript("return document.readyState").equals("complete"); 
     } 

    }; 
    wait.until(pageLoaded); 
} 

希望這會對你有用。

+0

沒有運氣,只要新選擇(driver.findElement(By.id(「searchId」)))。selectByVisibleText(「Search」);被執行,驅動程序拋出元素不可點擊的異常。 – maddie

+0

你在select語句之前調用了waitForPageLoad嗎? –

+0

我實際上在選擇語句後出現錯誤。我嘗試waitForPageLoad()之前點擊()過程按鈕。 – maddie