java - 使用Java拦截元素点击

标签 java selenium selenium-webdriver webdriver

控制台显示以下错误:

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)

我尝试过使用等待和 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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b1c2f3e282f1b3c363a323775383436" rel="noreferrer noopener nofollow">[email protected]</a>");
         
         
         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();
         
    }

}

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

最佳答案

您使用的 XPath 几乎是动态的,并且从不建议将 XPath 与索引一起使用。必须有一些属性可以用来编写提交按钮的 xpath。

你的 xpath 可能是这样的 -

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

关于java - 使用Java拦截元素点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65052441/

相关文章:

java - Selenium Web 驱动程序 日期选择器存在于发送键之后的页面中吗?

selenium - 如何自动化语音应用程序测试

java - PhantomJS 版本与 Selenium 的兼容性

java - 如何从任何 fragment 返回到主要 Activity ?

java - LOG4J - 避免在日志中出现两次相同的消息

java - 如何让 JScrollBar 在构造函数中采用长值?

java - 让 Jackson XMLMapper 在代码中设置根元素名称

node.js - Nightwatch JS - 如何通过 Firefox headless 运行测试

javascript - 如何使用javascript在selenium中执行Javascript并返回值

java - 如果两个值相同,则无法找到任一单选按钮