使用 Selenium Java 测试用例调用 CLICK 时获取 "org.openqa.selenium.ElementClickInterceptedException"
Getting "org.openqa.selenium.ElementClickInterceptedException" when invoking a CLICK using Selenium Java Test case
我正在使用下面的 java 代码和 Katalon Selenium IDE。
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public static void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\Users\myuser\Downloads\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
public static void testQScan() throws Exception {
driver.get("https://qualysguard.myorg.com/fo/login.php?idm_key=saml2_78743hhh43");
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='userNameInput']")));
System.out.println("Title of the page is 8 -> " + driver.getTitle());
driver.findElement(By.linkText("Scans")).click();
System.out.println("Title of the page is 9 -> " + driver.getTitle());
}
启动基础 URL 后,应该单击 "Scans" TAB。
这工作正常,使用 Selenium/Katalon 浏览器插件 IDE 播放时 "Scans" TAB 被点击。
但是,运行 导出的 java 代码给我以下错误:
输出:
Title of the page is 8 -> Dashboard
Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <a onclick="javascript: showProcessing('...');" href="/fo/tools/module_landing.php?module=prod_scans" class="module-link">Scans</a> is not clickable at point (180, 100). Other element would receive the click: <div id="page-loading-mask" style="visibility: visible;">...</div>
(Session info: chrome=77.0.3865.75)
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'AB-MYHOST7', ip: '10.9.9.112', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.75, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: C:\Users\myuser\AppData\Loc...}, goog:chromeOptions: {debuggerAddress: localhost:51849}, 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: 71b1daf7a06f6215547e7c79485c295e
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:423)
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 pack.QScan.testQualysScan(QScan.java:139)
at pack.QScan.main(QScan.java:212)
请提供有关如何克服该错误的建议。
浏览器上的“扫描”选项卡上的“查看源代码”具有以下条目:
<div class="clear"></div>
<div id="top_modules_bar">
<div class="module-tabs-tab module-tabs-tab-selected">
<a onclick="javascript: showProcessing('Dashboard');" href="/fo/compliance/index.php?skip=1" class="module-link">Dashboard</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Policies');" href="/fo/tools/module_landing.php?module=prod_policies" class="module-link">Policies</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Scans');" href="/fo/tools/module_landing.php?module=prod_scans" class="module-link">Scans</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Reports');" href="/fo/tools/module_landing.php?module=prod_reports" class="module-link">Reports</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Exceptions');" href="/fo/tools/module_landing.php?module=prod_exceptions" class="module-link">Exceptions</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Assets');" href="/fo/tools/module_landing.php?module=prod_assets" class="module-link">Assets</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Users');" href="/fo/tools/module_landing.php?module=prod_users" class="module-link">Users</a>
</div>
<div class="clear"></div>
</div>
使用操作 class 或 javascript 执行者:
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.linkText("Scans"))).click().perform();
或
try {
driver.findElement(By.linkText("Scans")).click();
} catch (Exception e) {
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.linkText("Scans")));
}
单击扫描诱导 WebDriverWait
和 elementToBeClickable()
并遵循 Xpath
选项
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='module-tabs-tab']//a[text()='Scans']"))).click();
或使用操作class点击。
WebElement scanEle =new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='module-tabs-tab']//a[text()='Scans']")));
Actions action =new Actions(driver);
action.moveToElement(scanEle).click().build().perform();
我正在使用下面的 java 代码和 Katalon Selenium IDE。
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public static void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\Users\myuser\Downloads\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
public static void testQScan() throws Exception {
driver.get("https://qualysguard.myorg.com/fo/login.php?idm_key=saml2_78743hhh43");
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='userNameInput']")));
System.out.println("Title of the page is 8 -> " + driver.getTitle());
driver.findElement(By.linkText("Scans")).click();
System.out.println("Title of the page is 9 -> " + driver.getTitle());
}
启动基础 URL 后,应该单击 "Scans" TAB。 这工作正常,使用 Selenium/Katalon 浏览器插件 IDE 播放时 "Scans" TAB 被点击。
但是,运行 导出的 java 代码给我以下错误:
输出:
Title of the page is 8 -> Dashboard
Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <a onclick="javascript: showProcessing('...');" href="/fo/tools/module_landing.php?module=prod_scans" class="module-link">Scans</a> is not clickable at point (180, 100). Other element would receive the click: <div id="page-loading-mask" style="visibility: visible;">...</div>
(Session info: chrome=77.0.3865.75)
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'AB-MYHOST7', ip: '10.9.9.112', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.75, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: C:\Users\myuser\AppData\Loc...}, goog:chromeOptions: {debuggerAddress: localhost:51849}, 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: 71b1daf7a06f6215547e7c79485c295e
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:423)
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 pack.QScan.testQualysScan(QScan.java:139)
at pack.QScan.main(QScan.java:212)
请提供有关如何克服该错误的建议。
浏览器上的“扫描”选项卡上的“查看源代码”具有以下条目:
<div class="clear"></div>
<div id="top_modules_bar">
<div class="module-tabs-tab module-tabs-tab-selected">
<a onclick="javascript: showProcessing('Dashboard');" href="/fo/compliance/index.php?skip=1" class="module-link">Dashboard</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Policies');" href="/fo/tools/module_landing.php?module=prod_policies" class="module-link">Policies</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Scans');" href="/fo/tools/module_landing.php?module=prod_scans" class="module-link">Scans</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Reports');" href="/fo/tools/module_landing.php?module=prod_reports" class="module-link">Reports</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Exceptions');" href="/fo/tools/module_landing.php?module=prod_exceptions" class="module-link">Exceptions</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Assets');" href="/fo/tools/module_landing.php?module=prod_assets" class="module-link">Assets</a>
</div>
<div class="module-tabs-tab">
<a onclick="javascript: showProcessing('Users');" href="/fo/tools/module_landing.php?module=prod_users" class="module-link">Users</a>
</div>
<div class="clear"></div>
</div>
使用操作 class 或 javascript 执行者:
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.linkText("Scans"))).click().perform();
或
try {
driver.findElement(By.linkText("Scans")).click();
} catch (Exception e) {
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.linkText("Scans")));
}
单击扫描诱导 WebDriverWait
和 elementToBeClickable()
并遵循 Xpath
选项
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='module-tabs-tab']//a[text()='Scans']"))).click();
或使用操作class点击。
WebElement scanEle =new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='module-tabs-tab']//a[text()='Scans']")));
Actions action =new Actions(driver);
action.moveToElement(scanEle).click().build().perform();