2017-10-09 112 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

相关问题