2017-05-26 65 views
0

我在版本3.4.0中使用了selenium-webdriver。即使Chrome无法加载页面,下面的代码也会打印成功,因为在端口3333上侦听的服务器尚未启动。为什么Selenium WebDriver解决了页面未加载时get()的承诺

const selenium = require('selenium-webdriver'); 
const webdriver = new selenium.Builder().forBrowser('chrome').build(); 
webdriver.get('http://localhost:3333').then(() => console.log('success')); 

回答

1

从文档(http://www.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations):

Dependent on several factors, including the OS/Browser combination, 
WebDriver may or may not wait for the page to load. In some 
circumstances, WebDriver may return control before the page has 
finished, or even started, loading. To ensure robustness, you need to wait 
for the element(s) to exist in the page using Explicit and Implicit Waits. 

所以,你想要么直接调用等待回调或创建一个承诺这样做:

webdriver.wait(function() { 
    webdriver.get('http://localhost:3333').then(() => console.log('success')); 
}, timeout); 

我离开用一个承诺来做这个练习来实现。

+0

你说得对,我在使用firefox驱动程序时承诺被拒绝。然而,你包含的代码是错误的 - 我们必须等待页面上存在一些元素,例如:'webdriver.get('http:// localhost:3333') .then(()=> webdriver.wait(until。 elementLocated(By.css( 'NAV'))));' – Marcin

-1

设置等待时间为5秒来加载页面或启动服务器。

webdriver.implicitly_wait(5); 
+0

页面无法加载,因为服务器不起作用。问题是硒会给出错误的反馈 - 成功而不是失败。 – Marcin