【问题标题】:Message "org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the XPath expression" using sendKeys使用 sendKeys 消息“org.openqa.selenium.InvalidSelectorException:无效选择器:无法使用 XPath 表达式定位元素”
【发布时间】:2018-09-29 04:47:32
【问题描述】:

我在尝试使用 Selenium 上传文件时遇到问题。由于浏览按钮没有输入标签,我无法使用 sendKeys() 函数传递路径。它是用 Angular 编写的。

这是浏览按钮元素:

<a id="attachmentUpload-browse0" name="attachmentUpload-browse0" ng-click="clickBrowse($index)" class="btn-pri" xpath="1"></a>

我确实在源代码中的上述代码正下方找到了一个输入标签,但在尝试使用 .sendKeys() 时出现错误。

<input type="file" id="attachmentUpload-file0" name="attachmentUpload-file0" fileread="$parent.attachments[$index].fileData" class="attachmentUpload-inputfile ng-isolate-scope" onchange="angular.element(this).scope().onFileChange(this)" style="" xpath="1">

编辑:使用 .sendKeys() 输入标签时出错

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //input[@id="attachmentUpload-file0"]// because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@id="attachmentUpload-file0"]//' is not a valid XPath expression.
*** Element info: {Using=xpath, value=//input[@id="attachmentUpload-file0"]//}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
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:545)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:319)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:421)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:311)
at com.ibm.esh.o2c.art.Tester.runTest(Tester.java:61)
at com.ibm.esh.o2c.art.Tester.main(Tester.java:70)

为附件上传浏览0使用标签时出错:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element

【问题讨论】:

  • 使用您在使用 &lt;input&gt; 标记时看到的 错误堆栈跟踪 更新问题
  • 会的。可能需要一点时间。 Selenium 在过去的一个小时里表现得很奇怪,页面加载需要很长时间。我在浏览器底部看到“正在连接”2 分钟(在 Chrome 中加载通常几乎是即时的)。前几天没有这个问题。
  • 好的,现在就到这里。
  • 尽量只使用driver.findElement(By.xpath("//input[@id='attachmentUpload-file0']")); element.sendKeys("test");
  • 这似乎奏效了。 driver.findElement(By.xpath("//input[@id='attachmentUpload-file0']")).sendKeys(System.getProperty("user.dir")+"/test1.csv");非常感谢。

标签: java selenium selenium-webdriver xpath webdriver


【解决方案1】:

此错误消息...

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //input[@id="attachmentUpload-file0"]// because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@id="attachmentUpload-file0"]//' is not a valid XPath expression.

...暗示您的 XPath 表达式不是有效的。

你可以使用下面这行代码:

driver.findElement(By.xpath("//input[@id='attachmentUpload-file0']")).sendKeys("test");

【讨论】:

    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2015-05-08
    • 2023-03-22
    • 2020-05-01
    • 1970-01-01
    • 2019-08-30
    相关资源
    最近更新 更多