1

我正在使用 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)
4

4 回答 4

1

click()在元素上,您必须诱导WebDriverWait并且elementToBeClickable()您可以使用以下任一Locator Strategies

  • 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'时,值得一提的是Selenium / 之间存在一些兼容性问题,您可以在以下位置找到详细讨论:


tl;博士

于 2019-09-23T11:38:32.540 回答
1

您收到一个ElementClickInterceptedException错误,这意味着页面上的某些其他元素与您尝试单击的元素重叠。您要么需要以某种方式与页面交互,以使重叠元素不再重叠,要么使用 JavaScript 来点击元素并触发“点击”事件。

许多网站都有随用户滚动的页面导航元素,因此浮动导航标题之类的东西可能会阻碍您要单击的元素。DebanjanB 有一个很好的解决方案作为解决此问题的下一步,但我怀疑您在等待元素可单击时会收到 TimeoutException。

您很可能需要观看此自动测试执行,然后在测试失败后使用该页面,然后再发现如何解决此问题。

于 2019-09-23T12:28:51.217 回答
0

你为什么不做以下点击?

WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
click.click()
于 2019-09-23T11:33:59.773 回答
0

可能您的 ID 正在发生变化。请在 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);
于 2019-09-23T11:37:36.633 回答