当前位置: 首页 > 知识库问答 >
问题:

Selenium:无法单击在使用find元素时找到的按钮,这在JS和Simple formatted way[duplicate]中都失败

罗绪
2023-03-14

我试图单击一个按钮,尝试使用其id、CSS和XPath,该按钮可通过findElement获得,但在单击时不执行操作。这是示例代码,也遵循异常。

  WebElement PageSpeedTestbutton1 = driver.findElement(By.cssSelector("#files")); 
            System.out.println("Found");    
            WebElement PageSpeedTestbutton2 = driver.findElement(By.xpath("//input[@id='files']"));
            System.out.println("Found");    
            PageSpeedTestbutton1.click();
            PageSpeedTestbutton2.click();
            System.out.println("Clicked Checker");



Results:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (-451, 258)
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'RAUNAK-MA', ip: '192.168.2.200', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.46.628402 (536cd7adbad73a..., userDataDir: C:\Users\RAUNAK~1.MAS\AppDa...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:64017}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 73.0.3683.103, webStorageEnabled: true}
Session ID: da5b790c67ddae03940ff612653dfbb7
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    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 newpackage1.newTest.main(newTest.java:87)

共有1个答案

孔斌
2023-03-14

以下错误表明您的元素可用,但不可单击。

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point 

尝试下面的代码,它应该可以工作。

WebElement PageSpeedTestbutton1 = driver.findElement(By.cssSelector("#files")); 

Actions action = new Actions(driver);
action.moveToElement(PageSpeedTestbutton1).click().build().perform();

WebElement PageSpeedTestbutton1 = driver.findElement(By.cssSelector("#files")); 

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", PageSpeedTestbutton1);

编辑

List<WebElement> PageSpeedTestbuttons = driver.findElements(By.cssSelector("#files"));
        if (PageSpeedTestbuttons.size()>0)
        {
            WebElement PageSpeedTestbutton1 =PageSpeedTestbuttons.get(0); 
            Actions action = new Actions(driver);
            action.moveToElement(PageSpeedTestbutton1).click().build().perform();
        }
        else
        {
            System.out.println("Element is not available");
        }
 类似资料: