java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) 在 Selenium 和 Java 中使用不正确的代码时 [英] java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) when using improper code with Selenium and Java
问题描述
为了快速制作原型,我使用了下面的 Selenium Webdriver Java 代码(实际上,它不符合最佳实践,但无论如何它应该可以工作,尽管不是最佳的).
if (!driver.findElements(By.xpath("//div[@class='ui-lib-popup-element__close']")).isEmpty()) {driver.findElement(By.xpath("//div[@class='ui-lib-popup-element__close']")).click();}
即它在 findElement()
中具有相同的值.当它被放入 while 循环时,它会在某个循环中抛出异常.
功能 {acceptInsecureCerts: true, browserName: firefox, browserVersion: 77.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20200602222727, moz:geckodriverVersion: 0.26.0, moz:headlessfalse, moz:processID: 12120, moz:profile: C:\Users\eljah32\AppData\Lo..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform:WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior:dismiss and notify}会话 ID:77ad55a3-7cc3-4126-a91d-f0b63c438520在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)在 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.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.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)在 SeleniumConfirmRNN.main(SeleniumConfirmRNN.java:122)
Selenium 库版本:
<依赖><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.141.59</version></依赖><!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver --><依赖><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-firefox-driver</artifactId><version>3.141.59</version></依赖><依赖><groupId>org.seleniumhq.selenium</groupId><artifactId>硒支持</artifactId><version>3.0.1</version></依赖>
那么,如何处理这个问题,它是一个错误吗?FirefoxDriver
会发生这种情况.
此错误信息...
功能 {acceptInsecureCerts: true, browserName: firefox, browserVersion: 77.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20200602222727, moz:geckodriverVersion: 0.26.0, moz:headlessfalse, moz:processID: 12120, moz:profile: C:\Users\eljah32\AppData\Lo..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform:WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior:dismiss and notify}会话 ID:77ad55a3-7cc3-4126-a91d-f0b63c438520在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
...暗示 GeckoDriver 无法 click()
在 WebElement在浏览上下文中,即Firefox浏览器会议.
您需要考虑以下几点:
- 首先,除非元素包含属性
contenteditable=,否则不要在
元素上调用click()
真实".<块引用>您可以在中找到详细讨论如何修改 contenteditable 元素的innerHTML
据推测,应该有一个子
或
元素,它是
中您想要的元素; 你指的是.因此,您需要更深入地定位 可交互 元素导致 WebDriverWait 用于elementToBeClickable()
并且您可以使用以下 定位器策略:尝试{new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ui-lib-popup-element__close']//input"))).click();System.out.println("元素被点击");}捕获(超时异常 e){System.out.println("元素未被点击");}
<小时>
参考
您可以在以下位置找到相关讨论:
For the sake of fast prototyping I have used the below Selenium Webdriver java code (in fact, it doesn't meet the best practices, but it should work anyway, despite of being non-optimal).
if (!driver.findElements(By.xpath("//div[@class='ui-lib-popup-element__close']")).isEmpty()) { driver.findElement(By.xpath("//div[@class='ui-lib-popup-element__close']")).click(); }
I.e. it has the same value in the
findElement()
. When it is put into the while loop, it throws at some loop the exceptions.Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 77.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20200602222727, moz:geckodriverVersion: 0.26.0, moz:headless: false, moz:processID: 12120, moz:profile: C:\Users\eljah32\AppData\Lo..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify} Session ID: 77ad55a3-7cc3-4126-a91d-f0b63c438520 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 SeleniumConfirmRNN.main(SeleniumConfirmRNN.java:122)
Selenium libraries versions:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-firefox-driver</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-support</artifactId> <version>3.0.1</version> </dependency>
So, what to do with this problem and is it a bug? That happens for
FirefoxDriver
.解决方案This error message...
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 77.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20200602222727, moz:geckodriverVersion: 0.26.0, moz:headless: false, moz:processID: 12120, moz:profile: C:\Users\eljah32\AppData\Lo..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify} Session ID: 77ad55a3-7cc3-4126-a91d-f0b63c438520 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
...implies that the GeckoDriver was unable to
click()
on the WebElement within the Browsing Context i.e. Firefox Browser session.
There are a couple of things you need to consider as follows:
- First of all, you don't invoke
click()
on a<div>
element unless the element contains the attributecontenteditable="true"
.
You can find a detailed discussion in How to modify the innerHTML of a contenteditable element
Presumably, there should be a child
<input>
or<span>
element which is your desired element within the<div>
you are referring. So you need to move one step deeper to locate the interactable element inducing WebDriverWait for theelementToBeClickable()
and you can use the following Locator Strategy:try { new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ui-lib-popup-element__close']//input"))).click(); System.out.println("Element was clicked"); } catch(TimeoutException e) { System.out.println("Element wasn't clicked"); }
Reference
You can find a relevant discussion in:
这篇关于java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) 在 Selenium 和 Java 中使用不正确的代码时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文- First of all, you don't invoke