【问题标题】:Automating Span with Selenium and Java 13使用 Selenium 和 Java 13 自动化 Span
【发布时间】:2019-09-23 11:31:30
【问题描述】:

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

【问题讨论】:

  • 你能发布你遇到的错误吗?
  • 没有错误,但不可点击
  • 我读到“自动化垃圾邮件”

标签: java selenium xpath css-selectors java-13


【解决方案1】:

click() 元素上你必须为elementToBeClickable() 诱导WebDriverWait 并且你可以使用以下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;博士

【讨论】:

  • @Ruban4Axis 请用错误堆栈跟踪更新问题
  • 更新了兄弟检查
  • @Ruban4Axis 查看更新的答案并告诉我状态。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    你为什么不做下面的点击呢?

    WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
    click.click()
    

    【讨论】:

      【解决方案4】:

      您的 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);
      

      【讨论】:

        猜你喜欢
        • 2018-06-14
        • 2018-06-13
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-04
        • 1970-01-01
        • 2016-11-11
        相关资源
        最近更新 更多