Selenium WebDriver wait无法正常工作
如果你打开这个链接 它会显示一个进度条之类的东西,然后你会在右上角看到Hotels选项。我也尝试过隐式等待、WebDriverWait和fluent等待。当我使用element.isDisplayed()时,它显示“true”,但当我执行单击操作时,它会抛出下面的异常Selenium WebDriver wait无法正常工作,selenium,selenium-webdriver,automation,wait,Selenium,Selenium Webdriver,Automation,Wait,如果你打开这个链接 它会显示一个进度条之类的东西,然后你会在右上角看到Hotels选项。我也尝试过隐式等待、WebDriverWait和fluent等待。当我使用element.isDisplayed()时,它显示“true”,但当我执行单击操作时,它会抛出下面的异常 driver.manage().window().maximize(); driver.get("https://www.phptravels.net/"); String hotelsX
driver.manage().window().maximize();
driver.get("https://www.phptravels.net/");
String hotelsXpth = "//ul[@class='main-menu go-left RTL']/li/a/span[contains(text(),'Hotels')]";
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath
(hotelsXpth)));
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(
hotelsXpth)));
System.out.println(driver.findElement(By.xpath(hotelsXpth)).isDisplayed());
driver.findElement(By.xpath(hotelsXpth)).click();
例外情况
org.openqa.selenium.WebDriverException: unknown error: Element <span>...
</span> is not clickable at point (705, 130). Other element would receive
the click: <div id="preloader" class="loader-wrapper">...</div>
(Session info: chrome=63.0.3239.132)
(Driver info: chromedriver=2.33.506120
(e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.14393
x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-
01T18:33:54.468Z'
System info: host: 'D-113060768', ip: '10.149.34.102', os.name: 'Windows
10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptSslCerts: true, applicationCacheEnabled: false,
browserConnectionEnabled: false, browserName: chrome, chrome:
{chromedriverVersion: 2.33.506120 (e3e53437346286..., userDataDir:
C:\Users\AS337139\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled:
false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true,
locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents:
true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform:
XP, platformName: XP, rotatable: false, setWindowRect: true,
takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: ,
unhandledPromptBehavior: , version: 63.0.3239.132, webStorageEnabled: true}
Session ID: bbfef8e4da0b2a6b98181d54c454d504
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)
org.openqa.selenium.WebDriverException:未知错误:元素。。。
在点(705130)处不可单击。其他要素将收到
点击:。。。
(会话信息:chrome=63.0.3239.132)
(驾驶员信息:chromedriver=2.33.506120
(E3E5343736286C0BC2D2DC9AA4915BA81D9023F),平台=Windows NT 10.0.14393
x86_64)(警告:服务器未提供任何堆栈跟踪信息)
命令持续时间或超时:0毫秒
构建信息:版本:'3.8.1',修订版:'6e95a6684b',时间:'2017-12-
01T18:33:54.468Z'
系统信息:主机:“D-113060768”,ip:“10.149.34.102”,os.name:“Windows”
10',os.arch:'amd64',os.version:'10.0',java.version:'1.8.0_121'
驱动程序信息:org.openqa.selenium.chrome.ChromeDriver
功能{acceptSslCerts:true,applicationCacheEnabled:false,
browserConnectionEnabled:false,browserName:chrome,chrome:
{chromedriverVersion:2.33.506120(E3E534376286…,userDataDir:
C:\Users\AS337139\AppData\L..},CSSSelectorEnabled:true,databaseEnabled:
false,handlesAlerts:true,hasTouchScreen:false,javascriptEnabled:true,
locationContextEnabled:true,mobileEmulationEnabled:false,nativeEvents:
true,networkConnectionEnabled:false,pageLoadStrategy:正常,平台:
XP,平台名称:XP,可旋转:false,setWindowRect:true,
takesHeapSnapshot:true,takesScreenshot:true,意外行为:,
未处理的PromptBehavior:,版本:63.0.3239.132,WebStorage Enabled:true}
会话ID:bbfef8e4da0b2a6b98181d54c454d504
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源)
在sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知)处
(来源)
位于java.lang.reflect.Constructor.newInstance(未知源)
有什么建议吗?我为一个本地项目写了这篇文章,该项目使用屏蔽方法,在页面上放置不可见的WebElements来阻止用户输入。这些元素将在页面“繁忙”时保留,并在准备接受输入时消失。修改下面的代码以检查类“预加载程序”而不是“blockUI”它应该对你有用
public static void waitForBlockUIToDisappear() {
// This function checks the entire currently-loaded web page for the
// presence of any web element of the
// class "blockUI" and loops until there are none. The presence of an
// element with this class does exactly
// what it implies: it blocks user input. If you get the error that a
// different web element would receive
// the click, for example, this is why and you'd need to call this
// method before doing a click. Generally,
// this function should be implemented before sending any input to a web
// page - click, sendkeys, select...
String blockUI = "//*[contains(@class,'blockUI')]";
while (true) {
if (driver.findElements(By.xpath(blockUI)).size() == 0)
break;
}
;
}
问题是Selenium知道Hotels链接是可见的,即使它位于“加载…”面板后面。因此,您等待Hotels链接可见、可单击等的尝试将立即通过,并且它将尝试单击,从而导致您看到的异常 解决方法是等待加载面板关闭,然后单击按钮。要做到这一点,您只需获取加载面板的定位器,然后等待它不可见 因为我假设你会经常点击酒店菜单(可能还有其他菜单),所以我会为你编写一个函数来处理这个问题
public static void clickMenu(String menu)
{
driver.findElement(By.xpath("//nav[@id='offcanvas-menu']//span[contains(.,'" + menu + "')]")).click();
}
脚本本身看起来像
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.id("preloader"))); // wait for Loading panel to close
clickMenu("Hotels");
我甚至可能会建议,因为您可能会重用代码以等待加载面板关闭,所以您也可以将其放入函数中。我将把它作为一个练习留给读者……:)以下链接的可能副本处理您的问题:@Ashraf Mulla,尝试从中实现解决方案,只需使用
wait.until(预期条件.不可见性FelementLocated(By.id(“预加载”));
问题不在于等待
,问题在于您的xpath
如果您传入要等待的定位器,而不是在函数中对其进行硬编码,那么它可能会更好。这样,它完全灵活/可重用。我同意。我在引用代码时确实考虑过这一点。当时在特定的项目中,只有一类拦截器。它们都是类名的变体,但它们都有共同的“blockUI”,因此包含用法。由于它是为只使用一个类而编写的应用程序,因此测试代码编写者不必知道阻止页面的类的名称,这使得测试代码编写变得更简单但我相信,在某些情况下,传递类名将是有益的,可能作为重载版本。这并不能回答问题。OP无法通过的启动屏幕,需要帮助。您的答案都是基于单击酒店菜单(可能还有其他菜单)经常,并不试图解决真正的问题。@DebanjanB如果你仔细阅读我的答案,你会在第二段中看到我回答了这个问题。它只有两句话。而且……第二段代码中的第一行解决了等待问题。这是经过测试的代码,它是有效的。@DebanjanB我回答了你的评论,你还没有撤销它你的否决票…为什么?