【问题标题】:InvalidArgumentException: invalid argument and InvalidSelectorException: invalid selector error with ChromeDriver and Chrome using Selenium with JavaInvalidArgumentException:无效参数和 InvalidSelectorException:ChromeDriver 和 Chrome 使用 Selenium 和 Java 时出现无效选择器错误
【发布时间】:2020-01-20 18:28:09
【问题描述】:

我以前从未遇到过这样的错误。我正在访问的网站是否对使用自动化测试有抵抗力?

代码:

public class DirectoryAutomation {

    public static void main(String[] args) throws IOException, InterruptedException {

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\MartinErlic\\eclipse-workspace\\DirectoryAutomation\\libs\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.arivify.com/property/az/mesa");
        Thread.sleep(2000);
        driver.findElement(By.xpath("xpath=//div[@id='__layout']/div/div[2]/section/div/table/tbody/tr/td")).click();
    }
}

例外:

Starting ChromeDriver 78.0.3904.105 (60e2d8774a8151efa6a00b1f358371b1e0e07ee2-refs/branch-heads/3904@{#877}) on port 21763
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1579544613.399][WARNING]: This version of ChromeDriver has not been tested with Chrome version 79.
[1579544615.411][WARNING]: Timed out connecting to Chrome, retrying...
Jan. 20, 2020 10:23:37 A.M. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument
  (Session info: chrome=79.0.3945.130)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-AS2OG7F', ip: '217.157.87.231', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '13.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 78.0.3904.105 (60e2d8774a81..., userDataDir: C:\Users\MARTIN~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:60322}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 613ee3b0588734b777f2ceb5d7715c9c
    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.RemoteWebDriver.get(RemoteWebDriver.java:277)
    at DirectoryAutomation.main(DirectoryAutomation.java:42)

更新异常:

Starting ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614}) on port 24206
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1579548408.283][WARNING]: Timed out connecting to Chrome, retrying...
Jan. 20, 2020 11:26:50 A.M. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression xpath=//a[contains(@href,'/property/search/hEC4ocSW0')] because of the following error:
TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type.
  (Session info: chrome=79.0.3945.130)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-AS2OG7F', ip: '192.168.1.75', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '13.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 79.0.3945.36 (3582db32b3389..., userDataDir: C:\Users\MARTIN~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:51045}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: c52709d658575c36b6705f1db4ce20b2
*** Element info: {Using=xpath, value=xpath=//a[contains(@href,'/property/search/hEC4ocSW0')]}
    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.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)
    at DirectoryAutomation.main(DirectoryAutomation.java:42)

【问题讨论】:

  • 您能否分享一个产生此异常的代码的最小示例?
  • @Julian 我用我正在使用的确切代码更新了我的帖子。
  • 请注意这里不需要 thread.sleep。 Selenium 将等待 pageready 状态...您似乎有一个无效的选择器。您发布的异常包括不应成为选择器一部分的“xpath=”。
  • 谢谢!就是这个。我懒洋洋地从 Selenium IDE 复制/粘贴,它不必要地将 css 附加到选择器:css=tr:nth-child(2) a。它应该只是tr:nth-child(2) a。 xpath 也是如此。需要删除xpath=。非常需要的健全性检查!谢谢。

标签: java google-chrome selenium-webdriver xpath invalidargumentexception


【解决方案1】:

如果您尝试使用相同代码(特别是使用相同的 webdriver 版本和 chrome 版本)的其他网站,是否会出现这种情况?

看到这个[1579544613.399][WARNING]: This version of ChromeDriver has not been tested with Chrome version 79.。告诉我您可能使用了不兼容的 driver.exe 和 chrome 版本。

看到您使用的是 chrome 版本 79,您应该获取与其匹配的 chromedriver 版本https://chromedriver.chromium.org/downloads,即79.0.3945.36。您目前正在使用78.0.3904.105

即使在 get(url) 方法上发生了抛出,这可能会使网站看起来是造成此错误的原因,但我相信这是巧合,问题将在浏览器的第一条指令上发生,不不管是哪种类型。

编辑。这看起来像是一个新问题。验证您的 xpath 是否指向有效元素,并且页面是否已完全加载。为了测试,我建议尝试将更简单的 xpath 定位到靠近根元素的页面的持久部分,以验证定位是否正常工作。此异常表明您已全部设置完毕,并且您需要开始调整该站点以获取时间和定位工作

【讨论】:

  • 嗨,我希望是这种情况......不幸的是,我将 ChromeDriver 更新为您提供的链接,虽然警告现在消失了,但同样的错误仍然存​​在。我确实尝试在其他网站上使用相同的代码,但仍然遇到相同的错误。
  • 您能否编辑您的问题以在底部包含当前日志输出以进行比较,以防有更新的线索隐藏在其中?
  • @MartinErlic 我还看到有一个 chromedriver 版本 80。您应该确认您没有可用的 chrome 更新。如果您使用的是旧版本,则有时可能会在以下主要更新中修复遗留的错误。
  • 嘿,我已经用新的例外更新了我的问题。 Chrome 80 还没有正式发布,所以可能是提前发布。这个例外实际上现在似乎有所不同......Exception in thread "main" org.openqa.selenium.InvalidSelectorException。但我试图选择整个页面的元素,但它们似乎都没有响应。
  • @MartinErlic 我更新了我的答案,但看起来你也注意到了。我认为您的浏览器问题已解决,并且您处于调整代码的范围内。我不确定您对硒的体验如何,所以如果我很迂腐,请原谅我。 webdriver 移动得很快,真的很快。它尝试并且有时开箱即用,效果很好,但通常 selenium 不会等待页面加载,并且在执行操作之前渲染 dom 的所有部分,这就是你的调整进来的地方,以减慢它并给出等待它是可靠的正确的事情
【解决方案2】:

第一条警告信息...

[WARNING]: This version of ChromeDriver has not been tested with Chrome version 79

...是由于您尝试启动/生成新的 浏览上下文(即 Chrome 浏览器时二进制文件版本之间的不兼容 strong> 会议:

  • chrome=79.0.3945.130
  • chromedriverVersion:78.0.3904.105

看来您已经通过将 ChromeChromeDriver 升级到相同级别来解决它。

您可以在This version of ChromeDriver has not been tested with Chrome version 79 error running protractorE2E tests with ChromeDriver Chrome Selenium找到详细讨论


第二条错误信息...

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression xpath=//a[contains(@href,'/property/search/hEC4ocSW0')] because of the following error:
TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type.

...暗示您构造的Locator Strategy 不是有效的定位器。

从您的代码尝试中,xpath 非常明显:

driver.findElement(By.xpath("xpath=//div[@id='__layout']/div/div[2]/section/div/table/tbody/tr/td")).click();

格式错误。


解决方案

根据指南,理想的xpath 表达式应该是:

driver.findElement(By.xpath("//div[@id='__layout']/div/div[2]/section/div/table/tbody/tr/td")).click();

您可以在InvalidSelectorError: invalid selector: Unable to locate an element with the xpath expression找到详细讨论

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2020-03-21
    • 2016-08-18
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多