java - WebDriver 在以下情况下忽略 Xpath 内容:

标签 java selenium xpath selenium-webdriver

这是我的 xpath:

(//td['displaytablecell']/span[text()='Name1']/../preceding-sibling::td[@class='ui-selection-column']/p-dtcheckbox/div/div[1]/input)[1]

Selenium WebDriver 代码忽略上面 xpath 中::之后的所有内容。

下面是当我使用 xpath 作为定位器来识别页面中的元素时,我在 selenium 中遇到的错误。

Exception occured: org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression (//td['displaytablecell']/span[text()='Name1']/../preceding-sibling because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '(//td['displaytablecell']/span[text()='Name1']/../preceding-sibling' is not a valid XPath expression.

关于如何使 webdriver 获取上面的整个 xpath 有什么想法或建议吗?

下面是 HTML 代码:

<td class="ui-selection-column" ng-reflect-ng-class="[object Object]" ng-reflect-ng-style="[object Object]" style="width: 38px; display: table-cell;">
<!--template bindings={ "ng-reflect-ng-if": "true" }-->
    <span class="ui-column-title"/>
    <p-dtcheckbox>
        <div class="ui-chkbox ui-widget">
            <div class="ui-helper-hidden-accessible">
                <input type="checkbox"/>
            </div>
            <div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default" ng-reflect-klass="ui-chkbox-box ui-widget ui-corner-all ui-state-default" ng-reflect-ng-class="[object Object]">
        </div>
    </p-dtcheckbox>
</td>
<!--template bindings={ "ng-reflect-ng-if": "true" }-->
<td ng-reflect-ng-class="[object Object]" style="display: table-cell;">

下面是 WebDriver Java 代码:

    @Test
        public void test_byxpath(){

            WebDriver driver = new ChromeDriver();

            driver.get("http://somesite.com");

            WebElement element = driver.findElement(By.xpath("(//td[@style='display: table-cell;']/span[text()='Name1']/../preceding-sibling::td[@class='ui-selection-column']/p-dtcheckbox/div/div[2])[1]"));    
                System.out.println("Test7 number of elements: " + elements.size());
          element.click();
}

元素是一个复选框,我尝试通过应用方法 .click() 来检查它。

下面是我在 Eclipse 中得到的结果:

Retrieving object of type 'xpath' and value '(//td[@style='display' from the object map
2017-01-25 16:52:09 -- INFO  automation.utilities.EventHandlerListener:102 - Trying to Find By.xpath: (//td[@style='display.
2017-01-25 16:52:10 -- INFO  automation.utilities.EventHandlerListener:98 - Exception occured: org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression (//td[@style='display because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '(//td[@style='display' is not a valid XPath expression.
  (Session info: chrome=53.0.2785.143)

最佳答案

如果这不起作用..那么也许尝试不同的 xpath: //td[span[text()='Name1']]/preceding-sibling::td[@class='ui-selection-column']//div[contains(@class,'ui-chkbox) -box')/]

关于java - WebDriver 在以下情况下忽略 Xpath 内容:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41860762/

相关文章:

java - 用于两种不同 URL 模式的 Spring mvc Swagger UI 端点

java - 为什么我不断收到 [line : 75] Error:must implement the inherited abstract method java. awt.event.ActionListener?

node.js - 组织3个环境的测试用例

python - 属性错误: 'str' object has no attribute 'descendants'

perl - 在日志中显示错误并继续抓取其他网址

java - Solr IP 地址字段类型

java - 如何为 container.listBlobs() 和 blob.exists() 生成单个帐户 SAS

python-3.x - python Selenium : How to use debugger_address option in chrome driver for Remote Debugging?

XPath - 基于多个子选择选择父节点

python - 如何攻克listing网站的honeypot listing? (抓取)