当前位置: 首页 > 知识库问答 >
问题:

消息“org.openqa.selenium.InvalidSelectorException:无效选择器:无法使用sendKeys定位XPath表达式的元素”

向安福
2023-03-14

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

以下是浏览按钮元素:

<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

共有1个答案

庄博厚
2023-03-14

此错误消息...

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");
 类似资料:
  • //div/SPAN[包含(text(),“默认值”)]/祖先::div/SPAN[包含(text(),“选择日期”)]/follows-sibling::div//输入 Xpath 2返回3个元素,即图像中的下拉项。 //div[@class='_selectcontainer_psmgei']/div//输入 截图:视窗截图 HTML屏幕截图:HTML代码

  • 嗨,我是一个相当新的硒。可以有人请建议如何定位和元素在iframe asi是得到错误下面。 带有Iframe标记的DOM按钮元素的屏幕截图 以前编写的代码

  • 下面是selenium找不到元素的代码。 但不知何故,它失败了,对我不起作用。它总是给我以下例外情况:过时的元素引用:元素未附加到页面文档 添加了--GetElementStextList的代码 感谢任何帮助。谢谢

  • 我是Selenium WebDriver的新手,使用NetBeans java和Firebug for Firefox获取XPath。问题是,即使某些元素在浏览器中是可见的和可点击的,并且firebug找到了xpath,我仍然得到一个错误“无法定位元素”。问题是,元素不在框架中(没有框架),不在另一个窗口中,它是可见的,可点击的,并且它有清晰的XPath。我用了Wait和thread.sleep,

  • 我试图引用并单击网页上的元素。 我们应该能够使用简单的XPath进行引用。例如 然而,这似乎并不奏效。 我使用的chrome扩展表示该元素不存在。 我的代码找不到元素。 下面是我使用Selenium web驱动程序的Java代码。 以下是错误: 太奇怪了!关于为什么我不能引用元素,或者为什么xPath不exist的任何想法。

  • 正在获取以下异常消息: Selenium 2.47.1不支持边缘驱动程序instace使用xpath定位元素???请您提供解决方案?