通过Selenium和Java使用findElement(By.partialLinkText())使用sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)

时间:2019-10-02 22:22:06

标签: java selenium selenium-chromedriver

我正在尝试从以下HTML代码获取文本“ Title”:

<a href="Link1" target="_blank">Title</a>

理想情况下,我想按部分链接文字进行搜索

System.out.println(driver.findElement(By.partialLinkText("Link1")).getText())

但是我也尝试使用其他方法来定位元素

我尝试通过chrome给定的完整Xpath进行查找:

driver.findElement(By.xpath("/html/body/div[1]/div[2]/form/table/tbody/tr[2]/td/div/"
                    + "section/section/div/article[2]/div[1]/div[2]/dl[1]/dd[1]/a")

我尝试按类名,CSS选择器和链接文本查找。他们都返回此错误

*** Element info: {Using=xpath, value=/html/body/div[1]/div[2]/form/table/tbody/tr[2]/td/div/section/section/div/article[2]/div[1]/div[2]/dl[1]/dd[1]/a}
    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.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)

一切似乎都是最新的(Chrome,Selenium Webdriver,JDK)

对类似的频繁帖子表示歉意,我试图简化我的问题,并使问题变得更糟。

0 个答案:

没有答案