有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何构造一个xpath,以单击以文本作为操作的元素

<div class="toolbar">
   <table>
      <tbody>
         <tr>
            <td class="text-button menu-button-active" itemid="sdfsgsg.0978" title="Actions" norap="">
               Actions
               <img src="../common/ads.gif">
            </td>
         </tr>
      </tbody>
   </table>  

img是td的子标记,类属性值是动态的

错误日志:

org.openqa.selenium.NoSuchElementException: Unable to locate element: //div[@class='toolbar']//td[text()='Actions']
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:32:14.902Z'
 os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 52.9.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:processID: 9268, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, specificationLevel: 0, timeouts: {implicit: 0, page load: 300000, script: 30000}}
Session ID: 0f4515d3-fad9-4c70-b50a-27f6c9abd249
*** Element info: {Using=xpath, value=//div[@class='toolbar']//td[text()='Actions']}
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    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:548)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:424)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:314)
    at PCS.PCS_TEst.BaseClass.CreateColor(BaseClass.java:100)

共 (5) 个答案

  1. # 1 楼答案

    因为文本中有空格,所以需要使用normalize-space清除它们:

    //div[@class='toolbar']//td[normalize-space(.)='Actions']
    
  2. # 2 楼答案

    如果imgtd的子元素,请尝试以下方法

    //td[@class="text-button menu-button-active"]//img
    

    如果imgtd的sibling元素,那么试试这个

    //td[@class="text-button menu-button-active"]/following-sibling::img
    

    希望这有帮助:)

  3. # 3 楼答案

    Xpath是:

    //div[@class='toolbar']//td[text()='Actions']
    
  4. # 4 楼答案

    如果你只想要“td”元素,那么使用css获得它,比如:

    WebElement actionsElement = driver.findElement(By.cssSelector("div.toolbar td"));
    actionsElement.click();
    

    如果你想点击内部图像元素,可以使用:

    WebElement imageElement = driver.findElement(By.cssSelector("div.toolbar td img"));
    imageElement.click();
    

    希望这对你有帮助

    使用xpath: 对于行动“td”元素:

    //td[@title='Actions']
    

    对于内部img元素:

    //td[@title='Actions']//img
    
  5. # 5 楼答案

    根据HTML,您可以使用以下xpath

    //td[@class='text-button menu-button-active' and @title='Actions'][normalize-space()='Actions']
    

    更新:

    此错误消息

    org.openqa.selenium.NoSuchElementException: Unable to locate element: //div[@class='toolbar']//td[text()='Actions']
    Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:32:14.902Z'
     os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    

    。。。这意味着壁虎河无法通过定位策略//div[@class='toolbar']//td[text()='Actions']定位元素

    根据你分享的HTML,你需要通过WebDriverWait诱导一个服务员,你可以使用以下解决方案:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='text-button menu-button-active' and @title='Actions'][normalize-space()='Actions']"))).click();
    

    但是您的主要问题是您正在使用的二进制文件版本之间的不兼容,如下所示:

    • 你的JDK版本1.8.0_131,非常古老

    解决方案:

    • 将JDK升级到最近的级别JDK 8u171
    • 执行@Test