2017-10-09 114 views
0

我正在用Selenium進行一些測試,並且測試必須登錄到系統。此登錄需要17秒鐘才能完全發生。 系統必須等待它完成,否則整個測試將失敗。Selenium不等待頁面加載

我已經嘗試了很多方法來做到這一點,但都失敗了。我試過

第一個代碼是這樣的:

driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); 

當我使用的是,即使我告訴它要等待100秒,我得到一個超時2(這幾乎是滿2分鐘!)幾秒鐘後用這個堆棧跟蹤。

org.openqa.selenium.WebDriverException: timeouts 
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown' 
System info: host: 'CMTCLX62137', ip: '53.19.227.206', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31' 
Driver info: org.openqa.selenium.firefox.FirefoxDriver 
Capabilities [{moz:profile=C:\Users\ALEX\AppData\Local\Temp\rust_mozprofile.Z2KJE568nWB8, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, moz:headless=false, platform=ANY, proxy=Proxy(manual, http=localhost), specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=56.0, platformVersion=6.1, moz:processID=21116.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}] 
Session ID: b2dca4a5-623a-4311-ad07-6444785dbcaf 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45) 
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) 
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637) 
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteTimeouts.implicitlyWait(RemoteWebDriver.java:868) 

另一個代碼,我已經試過:

new WebDriverWait(driver, 100).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript(
     "return document.readyState").equals("complete")); 

利用這一點,它只是不等待,我得到一個

org.openqa.selenium.NoSuchElementException: Unable to locate element 

的唯一方式爲我的測試工作是使用Thread.sleep(),但這是一個非常糟糕的選擇,因爲它有時會比預期的加載速度更快,有時仍會失敗,因爲它需要超過17秒。

等待頁面完全加載的其他選項?

+0

Selenium始終等待,直到頁面加載完畢。請發佈導致錯誤的所有代碼,以便我們可以進行調查。 – Buaban

回答

2

這是已經在這裏解決:Selenium wait until document is ready

反正我通常等待,而不是等到整個頁面加載,因爲我需要使用控件,:

wait.until(ExpectedConditions.elementToBeClickable(By 
      .id(ConfigData.IDs.buttonLogin))); 
+0

對不起,錯誤的答覆評論。這是行得通的。謝謝。 –

+0

很高興爲你效勞:) –

+1

@IgorÁvila好奇地知道什麼是'ConfigData.IDs'。我在課題中沒有看到類似的內容。 – DebanjanB

0

我猜使用elementToBeClickable()用明確的等待,而不是的頁面加載

WebElement ele= driver.findElement("Locator Value"); 
WebDriverWait wait=new WebDriverWait(driver, 20); 
wait.until(ExpectedConditions.elementToBeClickable(ele)); 
ele.click(); 
+0

使用此代碼仍然會收到「org.openqa.selenium.NoSuchElementException:無法找到元素:」。 –

+0

可以粘貼的HTML代碼或URL – iamsankalp89

+0

無論如何,我想你有答案。 – iamsankalp89

相關問題