使用 Selenium 和 Java 13 实现自动化 Span
Automating Span with Selenium and Java 13
我正在 Java.
使用 selenium 自动化网站
<a id="pd-vote-button10359300" class="css-vote-button pds-vote-button"><span>Vote</span></a>
对于这个按钮,我需要在 Selenium 中自动点击。我是跟着做的,不工作。
WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", click);
请问问题是什么?
之后
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))).click();
我遇到跟随错误
Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <span>...</span> is not clickable at point (122, 877). Other element would receive the click: <div class="nc_wrapper swp_floating_horizontal_wrapper bottom" style="background-color: rgb(255, 255, 255);">...</div>
(Session info: chrome=77.0.3865.90)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'Sanjeevans-iMac.local', ip: '169.254.10.5', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '13'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.90, chrome: {chromedriverVersion: 76.0.3809.126 (d80a294506b4..., userDataDir: /var/folders/k2/8cltlrwj23n...}, goog:chromeOptions: {debuggerAddress: localhost:59693}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 02cb57fb86be956e5e10be634b5724b1
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
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 bigboss.A.main(A.java:49)
为什么不执行以下操作来点击?
WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
click.click()
您的 ID 可能 change.Try 低于 xpath。
//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']
代码:
WebElement click = driver.findElement(By.xpath("//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", click);
To click()
在元素上你必须为 elementToBeClickable()
引入 WebDriverWait 并且你可以使用以下任一 :
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))).click();
更新
作为替代方法,您可以使用 executeScript()
方法,如下所示:
cssSelector
:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))));
xpath
:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))));
注意:由于您正在使用java.version: '13'
,值得一提的是 and java-11 / java-12之间存在一些兼容性问题,您可以找到详细的讨论在:
- Error occurred during initialization of boot layer when executing Selenium tests using TestNG and Java 12 through Eclipse
tl;博士
您收到 ElementClickInterceptedException
错误,这意味着页面上的某些其他元素与您尝试单击的元素重叠。您将需要与页面进行一些交互,以便重叠元素不再重叠,或者使用 JavaScript 来同时单击该元素并触发 "click" 事件。
许多网站都有随用户滚动的页面导航元素,因此浮动导航 header 之类的东西可能会挡住您要单击的元素。 DebanjanB 有一个很好的解决方案作为解决此问题的下一步,但我怀疑您在等待元素可点击时会遇到 TimeoutException。
您很可能需要观看此自动化测试的执行过程,然后在测试失败后试玩页面,然后再了解如何解决此问题。
我正在 Java.
使用 selenium 自动化网站<a id="pd-vote-button10359300" class="css-vote-button pds-vote-button"><span>Vote</span></a>
对于这个按钮,我需要在 Selenium 中自动点击。我是跟着做的,不工作。
WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", click);
请问问题是什么?
之后new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))).click();
我遇到跟随错误
Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <span>...</span> is not clickable at point (122, 877). Other element would receive the click: <div class="nc_wrapper swp_floating_horizontal_wrapper bottom" style="background-color: rgb(255, 255, 255);">...</div>
(Session info: chrome=77.0.3865.90)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'Sanjeevans-iMac.local', ip: '169.254.10.5', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '13'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.90, chrome: {chromedriverVersion: 76.0.3809.126 (d80a294506b4..., userDataDir: /var/folders/k2/8cltlrwj23n...}, goog:chromeOptions: {debuggerAddress: localhost:59693}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 02cb57fb86be956e5e10be634b5724b1
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
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 bigboss.A.main(A.java:49)
为什么不执行以下操作来点击?
WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
click.click()
您的 ID 可能 change.Try 低于 xpath。
//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']
代码:
WebElement click = driver.findElement(By.xpath("//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", click);
To click()
在元素上你必须为 elementToBeClickable()
引入 WebDriverWait 并且你可以使用以下任一
cssSelector
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))).click();
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))).click();
更新
作为替代方法,您可以使用 executeScript()
方法,如下所示:
cssSelector
:((JavascriptExecutor) driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))));
xpath
:((JavascriptExecutor) driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))));
注意:由于您正在使用java.version: '13'
,值得一提的是
- Error occurred during initialization of boot layer when executing Selenium tests using TestNG and Java 12 through Eclipse
tl;博士
您收到 ElementClickInterceptedException
错误,这意味着页面上的某些其他元素与您尝试单击的元素重叠。您将需要与页面进行一些交互,以便重叠元素不再重叠,或者使用 JavaScript 来同时单击该元素并触发 "click" 事件。
许多网站都有随用户滚动的页面导航元素,因此浮动导航 header 之类的东西可能会挡住您要单击的元素。 DebanjanB 有一个很好的解决方案作为解决此问题的下一步,但我怀疑您在等待元素可点击时会遇到 TimeoutException。
您很可能需要观看此自动化测试的执行过程,然后在测试失败后试玩页面,然后再了解如何解决此问题。