我试图单击一个按钮,尝试使用其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)
以下错误表明您的元素可用,但不可单击。
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");
}
HTML: Java:
问题内容: HTML代码: 使用类名,我试图单击按钮,但无法单击。 我尝试了以下代码: 问题答案: 您可以尝试通过获取它。据我所记得,这只是一堂课。
我试图从一个网站获取一个元素,我会得到这个错误: 我通过在inspect element中复制XPath来获取元素,并确保元素存在且可点击。以下是我的代码: 以下是代码: 我该怎么解决这个问题?
问题内容: 我正在使用Java学习Selenium Webdriver。作为一个学习示例,我尝试打开MakeMyTrip,访问“ 国际航班”页面,然后单击Google Chrome中的“ 单向” 单选按钮。我尝试了其他方法来定位此单选按钮,但仍无法正常工作。请在我的代码示例下面找到。 谁能帮我解决这个问题? 问题答案: 使用以下代码:- 享受..如果仍然遇到任何问题,请与我联系:)
我还在学习硒,但下面是我尝试过的: 我得到了这些错误: 而且
无法使用driver.find_element_by_css_selector单击按钮 我尝试使用此查找元素driver.find_element_by_css_selector