使用Selenium和Java13自动化Span

使用Selenium和Java13自动化Span,java,selenium,xpath,css-selectors,java-13,java-11,java-12,Java,Selenium,Xpath,Css Selectors,Java 13,Java 11,Java 12,我正在用Java中的selenium自动化一个网站 <a id="pd-vote-button10359300" class="css-vote-button pds-vote-button"><span>Vote</span></a> 你能告诉我是什么问题吗 之后 new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@

我正在用Java中的selenium自动化一个网站

<a id="pd-vote-button10359300" class="css-vote-button pds-vote-button"><span>Vote</span></a>
你能告诉我是什么问题吗

之后

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)
线程“main”org.openqa.selenium.element中的异常ClickInterceptedException:element单击intercepted:element。。。在点(122877)处不可单击。其他元素将收到单击:。。。 (会话信息:chrome=77.0.3865.90) 构建信息:版本:“3.141.59”,修订版:“e82be7d358”,时间:“2018-11-14T08:25:48” 系统信息:主机:'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' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能{AcceptSecureCerts:false,browserName:chrome,browserVersion:77.0.3865.90,chrome:{chromedriverVersion:76.0.3809.126(d80a294506b4…,userDataDir:/var/folders/k2/8cltlrwj23n…),goog:chromeOptions:{调试地址:本地主机:59693},javascriptEnabled:true,networkConnectionEnabled:false,pageLoadStrategy:normal,platform:MAC,platformName:MAC,proxy:proxy(),setWindowRect:true,StrictFileInteractivity:false,超时:{隐式:0,pageLoad:300000,脚本:30000},未经处理的PromptBehavior:Disease and notify} 会话ID:02cb57fb86be956e5e10be634b5724b1 位于java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 位于java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 位于java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) 位于java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) 位于org.openqa.selenium.remote.http.w3chttprosecodec.createException(w3chttprosecodec.java:187) 在org.openqa.selenium.remote.http.w3chttrpresponsecodec.decode(w3chttrpresponsecodec.java:122) 位于org.openqa.selenium.remote.http.w3chttprosecodec.decode(w3chttprosecodec.java:49) 位于org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) 位于org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 位于org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) 位于org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285) 在org.openqa.selenium.remote.RemoteWebElement.click上(RemoteWebElement.java:84) 在bigboss.A.main(A.java:49)
为什么不执行以下操作来单击

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

可能您的ID正在更改。请尝试下面的xpath

//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']
代码:

单击元素上的()
,您必须为
元素导入WebDriverWait以使其可伸缩()
,并且您可以使用以下任一选项:

  • css选择器

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))).click();
    
    ((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

    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();
    
    ((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']"))));
    

更新 您也可以使用
executeScript()
方法,如下所示:

  • css选择器

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))).click();
    
    ((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

    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();
    
    ((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'
时,值得一提的是和/之间存在一些兼容性问题,您可以在以下内容中找到详细讨论:


太长,读不下去了

您收到一个
元素ClickInterceptedException
错误,这意味着页面上的其他元素与您尝试单击的元素重叠。您需要与页面进行交互,以使重叠的元素不再重叠,或者使用JavaScript同时单击该元素并触发“单击”事件

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


您很可能需要观看此自动测试的执行,然后在测试失败后播放页面,然后才能发现如何修复此问题。

您可以发布您收到的错误吗?没有收到错误。但不可单击我阅读“自动垃圾邮件”@Ruban4Axis使用stacktrace Please更新的Bro错误更新问题Check@Ruban4Axis签出更新的答案并让我知道状态。