通过Selenium和Java使用findElement(By.className())的sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) [英] sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) using findElement(By.className()) through Selenium and Java

查看:230
本文介绍了通过Selenium和Java使用findElement(By.className())的sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下代码时:

driver.findElement(By.className("qview-product-name")).click();

我收到以下错误

Session ID: d5df6f837164b1738991e8dc09027fe0
*** Element info: {Using=class name, value=qview-product-name}
    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.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.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByClassName(RemoteWebDriver.java:412)
    at org.openqa.selenium.By$ByClassName.findElement(By.java:389)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at Logins.bcLogin(Logins.java:140)
    at Exception.main(Exception.java:54)

我正在处理的网页肯定包含以下HTML代码,并且我尝试等待适当的时间来执行.

The webpage I am working on definately contains the following HTML code and I have tried waiting an appropriate amount of time to execute.

<dd class="qview-product-name">
    <span class="note">1 x </span>
                    <a href="Link_here"_blank">Title</a>
            </dd>

我以为我可以很好地使用各种方法来定位元素,但这让我很困惑.关于如何进行故障排除的任何想法?谢谢!

I thought I was getting pretty good at locating elements using the various methods, but this has me stumped. Any ideas on how I should go about troubleshooting? Thanks!

推荐答案

您需要注意以下几点:

  • By.className("qview-product-name")引用父< dd> 标记,可能不是您想要单击的元素.相反,您的用例必须是单击< a href ="Link_here" _blank> Title</a> 元素.
  • 按照最佳做法,在调用 click()时,您需要诱导为 elementToBeClickable()诱导 WebDriverWait >,您可以使用以下任一定位器策略:

  • By.className("qview-product-name") refers to the parent <dd> tag and perhaps is not the desired element you want to click. Rather your usecase must be to click on the <a href="Link_here"_blank">Title</a> element.
  • As per best practices, while invoking click() you need to induce you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

linkText :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Title"))).click();

  • cssSelector :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("dd.qview-product-name a[href='Link_here']"))).click();
    

  • xpath :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//dd[@class='qview-product-name']//a[@href='Link_here' and text()='Title']"))).click();
    

  • 确保:

    • JDK 已升级到当前级别JDK 8u222 .
    • 已升级到当前级别版本3.141.59 .
    • ChromeDriver 已更新为当前的ChromeDriver v77.0 级别.
    • Chrome 已更新为当前的 Chrome版本77.0 级别.(根据 ChromeDriver v77.0发行说明)
    • 通过 IDE
    • 清理您的项目工作区重建您的项目,并且仅具有必需的依赖项.
    • 如果您的基本 Web客户端版本太旧,则将其卸载并安装最新版本的 Web客户端 GA.
    • 进行系统重启.
    • 非root用户用户的身份执行 @Test .
    • JDK is upgraded to current levels JDK 8u222.
    • Selenium is upgraded to current levels Version 3.141.59.
    • ChromeDriver is updated to current ChromeDriver v77.0 level.
    • Chrome is updated to the current Chrome Version 77.0 level. (as per ChromeDriver v77.0 release notes)
    • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
    • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
    • Take a System Reboot.
    • Execute your @Test as non-root user.

    这篇关于通过Selenium和Java使用findElement(By.className())的sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆