这是我的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;">
以下是WebDriverJava代码:
@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();
}
元素是一个复选框,我试图通过应用方法来检查它。单击()。
下面是我在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”]/前面的同级::td[@ui-selection-column']//div[contains(@class,'ui-chkbox-box')/]