selenium元素click被java截获

8qgya5xd  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(429)

控制台显示以下错误:

Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378}) on port 12848
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1606582973.595][WARNING]: This version of ChromeDriver has not been tested with Chrome version 87.
Nov 28, 2020 10:32:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit button"> is not clickable at point (522, 587). Other element would receive the click: <span class="desktop-text">...</span>
  (Session info: chrome=87.0.4280.66)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'PLANET', ip: '192.168.0.106', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '12.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.66, chrome: {chromedriverVersion: 86.0.4240.22 (398b0743353ff..., userDataDir: C:\Users\Admin\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:56606}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 80bd603f6492d9dc7b4f71d2ae7d5edb
    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 MailTest.main(MailTest.java:46)

我尝试过使用waits和javascript executor,但没有发现这个错误。下面是我正在使用的代码:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class MailTest {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");

         WebDriver driver = new ChromeDriver();

        driver.get("testUrl");

        //Maximizing window
         driver.manage().window().maximize();

         driver.findElement(By.xpath("//body/div[@id='offer--modal']/div[1]/div[2]/p[1]")).click();
         Thread.sleep(10000);

         WebElement Name = driver.findElement(By.xpath("//input[@id='name']"));
         Name.sendKeys("test"); 

         WebElement email = driver.findElement(By.xpath("//input[@id='email']"));
         email.sendKeys("Gtest@gmail.com");

         WebElement Subject = driver.findElement(By.xpath("//input[@id='subject']"));
         Subject.sendKeys("Testing");

         driver.findElement(By.xpath("//span[contains(text(),'Mobile Development')]")).click();
         driver.findElement(By.xpath("//span[contains(text(),'Application Development')]")).click();

         driver.findElement(By.xpath("//textarea[@id='message']")).sendKeys("testing for automation");

     WebElement Sendbutton = driver.findElement(By.xpath("//body/div[4]/div[2]/div[1]/div[1]/div[1]/section[1]/div[1]/form[1]/div[7]/input[1]"));
         JavascriptExecutor executor = (JavascriptExecutor)driver;
         executor.executeScript("arguments[0].click()", Sendbutton);

        // driver.close();

    }

}

上面的脚本工作良好,直到提交按钮,我试图得到点击。另外,我觉得问题可能出在submit按钮的xpath上。我用chropath来寻找xpath。

uemypmqf

uemypmqf1#

您正在使用的xpath是非常动态的,不建议将xpath与索引一起使用。必须有一些属性,您可以使用这些属性为submit按钮编写xpath。
您的xpath可以是-

> //button[@class='submit']
r7s23pms

r7s23pms2#

您的xpath可以改进为: //input[@value='Send'] 但是真正的问题是这个按钮被这个元素阻塞了(正如元素clickinterceptedexception所指出的):

<span class="desktop-text">...</span>

因此,我们要么关闭此窗口,要么通过执行以下操作隐藏此元素:

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//span[@class='desktop-text']"));
js.executeScript("arguments[0].setAttribute('display', 'none')", element);

然后尝试单击:

WebElement Sendbutton = driver.findElement(By.xpath("//input[@value='Send']"));
Sendbutton.click()

相关问题