无效的Xpath表达式

时间:2017-05-13 13:50:58

标签: selenium xpath

我的Html就像:

<pre>
 <ul class="dropdown-menu dropdown-triangle-bottom-right">
  <li class="post-save-publish">
   <a href="#" data-ember-action="1774">Publish Now</a>
  </li>
  <li class="post-save-draft active">
   <a href="#" data-ember-action="1775">Save Draft</a>
  </li>
  <li class="divider delete"/>
   <li class="delete">
    <a href="#" data-ember-action="1776">Delete Post</a>
   </li>
 </ul>
</pre>

处理“立即发布”按钮的代码:

WebElement t=driver.findElement(By.xpath("//*[@href='#' AND text()='Publish Now']"))

我收到错误:

1494681211957   geckodriver INFO    Listening on 127.0.0.1:44241
1494681212176   mozprofile::profile INFO    Using profile path /var/folders/dg/glyvppvs1dn31sqrrgt7yhjc0000gn/T/rust_mozprofile.4mJ76F47MPkl
1494681212177   geckodriver::marionette INFO    Starting browser /Applications/Firefox.app/Contents/MacOS/firefox-bin with args []
1494681212181   geckodriver::marionette INFO    Connecting to Marionette on localhost:60723
[warn] kq_init: detected broken kqueue; not using.: Undefined error: 0
1494681212732   Marionette  INFO    Listening on port 60723
May 13, 2017 6:43:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
JavaScript warning: http://localhost:2368/ghost/vendor.js?v=115d872ce7, line 2753: mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Given xpath expression "//*[@href='#' AND text()='Publish Now']" is invalid
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
System info: host: 'Javeds-MacBook-Pro.local', ip: 'fd50:1d9:9d9b:eb00:6d64:a5da:2f9c:7790', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.4', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, appBuildId=20160623154057, version=47.0.1, platform=MAC, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, browserVersion=47.0.1, platformVersion=16.5.0, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=Firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=Darwin, device=desktop}]
Session ID: 440c0fbb-62f8-ec43-9965-b3a154425a21
*** Element info: {Using=xpath, value=//*[@href='#' AND text()='Publish Now']}
        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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:133)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:99)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:43)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:476)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
        at TestingBlogWebsite.AdminWebsite.main(AdminWebsite.java:313)

2 个答案:

答案 0 :(得分:2)

您会收到错误,因为您应该使用小写的XPath运算符

"//*[@href='#' and text()='Publish Now']"

答案 1 :(得分:2)

尝试

//*[@href='#' and .='Publish Now']

//*[@href='#' and contains(text(), 'Publish Now')]