WebDriver在忽略Xpath内容之后::

时间:2017-01-25 20:09:57

标签: 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中的所有内容。

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

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)

1 个答案:

答案 0 :(得分:1)

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