gpt4 book ai didi

java - 使用 sendKeys 的消息 "org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the XPath expression"

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:08:40 26 4
gpt4 key购买 nike

我在尝试使用 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)

为 attachmentUpload-browse0 使用标签时出错:

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

最佳答案

这个错误信息...

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");

关于java - 使用 sendKeys 的消息 "org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the XPath expression",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49908244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com