问题
Hey All I have an issue that I am trying to resolve for long time. The problem is that I want selenium to wait for element to be clickable, after a message displayed on screen.
- The element ID is: header-account
- The timeout I sent to method is:15500
- I set the Implicit wait to: IMPLICIT_WAITE = 35;
- As you can see selenium not waits for a second
This is my main:
BasePage.waitInvisabilityOfElementByXpath("//div[@class='ant-notification ant-notification-topRight']");
System.out.println("\n\n Finished first wait \n\n ");
By locator;
locator = By.id("header-account");
BasePage.waitForClickable(locator,15500);
System.out.println("\n\n Finished second wait \n\n ");
LoginHomeManager.logoutNoSleep();
and I have this method I created:
public static void waitForClickable(By locator, int timeOut) throws Exception {
WebDriver driver = WebDriverMgr.getDriver();
driver.manage().timeouts().implicitlyWait(IMPLICIT_WAITE, TimeUnit.SECONDS);
Instant now = Instant.now();
while (Instant.now().isBefore(now.plusSeconds(timeOut)))
{
try
{
new WebDriverWait(driver, timeOut).until(ExpectedConditions.elementToBeClickable(locator));
return;
}
catch (WebDriverException e)
{
// do nothing, loop again
}
}
throw new Exception("Not able to click element <" + locator + "> within " + timeOut + "s.");
}
I get this error:
** Click Avatar by ID: header-account
Sun Oct 27 11:58:05 IST 2019:INFO: WebDriver: Click on [[ChromeDriver: chrome on XP (e22c7e2f4d7f1c4bb8abf664a7f53366)] -> id: header-account]
Sun Oct 27 11:58:05 IST 2019:ERROR: element click intercepted: Element <button _ngcontent-iib-c11="" class="avatar-style ant-btn ant-btn-default ant-btn-circle" id="header-account" nz-button="" nz-popover="" nzplacement="bottomRight" nzshape="circle" nztrigger="click" ng-reflect-nz-shape="circle" ng-reflect-nz-content="[object Object]" ng-reflect-nz-trigger="click" ng-reflect-nz-placement="bottomRight" ng-reflect-directive-name-title="" nz-wave="[object Object]">...</button> is not clickable at point (1888, 31). Other element would receive the click: <path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 0 0 203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"></path>
(Session info: chrome=78.0.3904.70)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'PC', ip: '10.9.25.7', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 78.0.3904.70, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: C:\Users\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:60679}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: e22c7e2f4d7f1c4bb8abf664a7f53366
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <button _ngcontent-iib-c11="" class="avatar-style ant-btn ant-btn-default ant-btn-circle" id="header-account" nz-button="" nz-popover="" nzplacement="bottomRight" nzshape="circle" nztrigger="click" ng-reflect-nz-shape="circle" ng-reflect-nz-content="[object Object]" ng-reflect-nz-trigger="click" ng-reflect-nz-placement="bottomRight" ng-reflect-directive-name-title="" nz-wave="[object Object]">...</button> is not clickable at point (1888, 31). Other element would receive the click: <path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 0 0 203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"></path>
(Session info: chrome=78.0.3904.70)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'PC', ip: '10.9.7.5', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 78.0.3904.70, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: C:\Users\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:60679}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: e22c7e2f4d7f1c4bb8abf664a7f53366
And then this exception occured:
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.lambda$new$0(EventFiringWebDriver.java:404)
at com.sun.proxy.$Proxy6.click(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.click(EventFiringWebDriver.java:417)
at com.blinkx.rx.ui.common.abstractionLayer.HomePage.clickAvatarNoSleep(HomePage.java:215)
at com.blinkx.rx.ui.common.abstractionLayer.HomePage.clickLogoutNoSleep(HomePage.java:221)
at com.blinkx.rx.ui.common.areaLibrary.LoginHomeManager.logoutNoSleep(LoginHomeManager.java:223)
at com.blinkx.rx.ui.common.tests.EXPublisherSignUp.FinalPUbOpsApproval(EXPublisherSignUp.java:244)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
In the consul it is printing the System.out.println("\n\n Finished second wait \n\n ");
Meaning it should be clickable and the function worked, however it is actually not worked.
This is the UI I want selenium to wait the message displayed and then press the circle icon and log out.
This is what I expected manually
The site is angular
回答1:
The element is partially visible, so ExpectedConditions.elementToBeClickable
pass. It checks if the element is visible (it is) and enabled(which is usually true).
Selenium tries to click in the center of the element, which is covered by the success message hence the exception
ElementClickInterceptedException: element click intercepted: Element <button id="header-account" is not clickable at point (1888, 31). Other element would receive the click: ...
You need to close the success message and wait for it to disappear before clicking the button
WebDriverWait wait = new WebDriverWait(driver, timeOut);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("i.anticon.ant-notification-close-icon.anticon-close > svg")));
element.click();
wait.until(ExpectedConditions.invisibilityOf(element));
来源:https://stackoverflow.com/questions/58578976/selenium-not-wait-to-element-to-be-clickable