【问题标题】:Selenium locator xpath not working for particular buttonSelenium 定位器 xpath 不适用于特定按钮
【发布时间】:2017-06-05 13:14:43
【问题描述】:

我正在寻找一个有效的定位器来定位网页上的按钮。

我看到的代码中的“检查元素”:

<input class="ruButton ruBrowse" tabindex="-1" value="Select & Upload File" style="border: 0px dotted;" type="button">

复制outerhtml我有:

<input tabindex="-1" value="Select &amp; Upload File" class="ruButton ruBrowse" style="border: 0px dotted;" type="button">

我尝试了以下作为 xpath 定位器,但这些都不好 - 我想了解原因:

//input[@value='Select & Upload File']
//input[@value='Select &amp; Upload File']

这个定位器没问题:

driver.findElement(By.xpath("//input[contains(@class,'ruButton ruBrowse') and contains(@type,'button')]")).click();

有关错误日志的更多详细信息: 1)

driver.get(baseUrl);
driver.findElement(By.xpath("//input[contains(@value,'Select') and contains (@value,'Upload File')]")).click();

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //input[contains(@value,'Select') and contains(@value,'Upload File')] (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 297 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'BGC-W7X64T-2082', ip: '10.119.137.75', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, pageLoadStrategy=normal, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:2351/, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss}]
Session ID: 4613c6bf-239c-4ccb-b3b3-f1946276e7c7
*** Element info: {Using=xpath, value=//input[contains(@value,'Select') and contains(@value,'Upload File')]}
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.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
at Selenium_And_AutoIt.main(Selenium_And_AutoIt.java:61)

2)

driver.get(baseUrl);
driver.findElement(By.xpath("//input[@value='Select & Upload File']")).click();

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //input[@value='Select & Upload File'] (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 281 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'BGC-W7X64T-2082', ip: '10.119.137.75', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, pageLoadStrategy=normal, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:21427/, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss}]
Session ID: eb3f9de0-2fc1-41ce-8f15-6947f956c216
*** Element info: {Using=xpath, value=//input[@value='Select & Upload File']}
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.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
at Selenium_And_AutoIt.main(Selenium_And_AutoIt.java:60)

谢谢!

【问题讨论】:

  • 你为什么认为these are not OK?显示异常日志
  • 嗨安德森,谢谢。线程“main”中的异常 org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //input[@value='Select & Upload File'] (警告:服务器没有提供任何堆栈跟踪信息)是 &需要在这里转义的特殊字符?
  • 您的 xpath 定位器是正确的。可以发一下页面的html源码吗?
  • ManishChristian,我很想这样做,但不幸的是我不知道如何做到这一点而不会出错。当我编辑我的帖子时,然后单击 {} 并将字符串“在此处输入代码”替换为完整的 html,不幸的是,它似乎不正确。有什么提示可以分享吗?谢谢。
  • @JanVandekerckhove,检查input 元素是否位于&lt;iframe&gt; 块内

标签: selenium xpath automated-tests


【解决方案1】:

对于它的价值,我过去在使用内部也有与号的文本时遇到了麻烦。我的解决方案是使用 contains 而不是“=”,并且只提供部分值文本,但是,这可能还不够,因为可能有一些其他元素包含例如“上传文件”。我在这种情况下的解决方案,假设定位器原样不正确,将是以下定位器:

//input[contains(@value,'Select') and contains(@value,'Upload File')]

它也可以相应地用'starts-with'和'ends-with'替换'contains'。

【讨论】:

  • 您好 Bill Hileman,感谢您与我一起思考并提出您的建议!我以前也试过这个,但不幸的是没有任何运气。对我来说奇怪的是:在另一种语言中(不存在“&”)定位器工作正常。
猜你喜欢
  • 1970-01-01
  • 2020-08-26
  • 2010-10-30
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 2011-05-19
  • 2021-04-14
  • 2022-01-05
相关资源
最近更新 更多