Geo*_*ith 6 selenium xpath selenium-webdriver
亲爱的Selenium Webdriver专家,
我想知道Selenium Webdriver中的字符串匹配方法是否与Java中的以下代码片段正常工作:
if (property.findElements(By.xpath("./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house']")).size() > 0 ) { // line 229
Run Code Online (Sandbox Code Playgroud)
以下是第229行读取的xhtml网页:
<dl class="cN-featDetails">
<dt class="proptype">Property type</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddPropertyType" class="propertytype type-house" title="Property type: House">House</dd>
Run Code Online (Sandbox Code Playgroud)
但是,这导致以下错误:
Address: 28B/171 Gloucester Street, Sydney
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector ./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house'] is either invalid or does not result in a WebElement. The following error occurred:
[InvalidSelectorError] Unable to locate an element with the xpath expression ./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house'] because of the following error:
[Exception... "The expression is not a legal expression." code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)" location: "
Run Code Online (Sandbox Code Playgroud)
我也尝试过matches(class,'propertytype.*$']")没有成功.
类的名称根据房产(房屋类型)或公寓(类型公寓)而变化......
有关如何在匹配中使用正则表达式的任何建议,以检查此属性类型元素中是否存在值/有效树节点?
此代码段正在查找此URL.
我在Windows XP和7平台上使用Selenium 2.25.0,Java 1.7.0_11.
非常感谢您的建议.
Pet*_*ček 11
不幸的是,该matches()功能是XPath 2.0的一部分.
WebDriver使用仅支持XPath 1.0 的Wicked Good XPath库.
因此,您的XPath表达式是非法的,您应该将其重写为仅使用XPath 1.0中的功能和函数.
我想你可以简单地用matches()你的例子替换这个电话contains().也就是说,通过匹配类名称并不是一个好习惯contains(),因为它type-house也会匹配type-houses.此外,如果您匹配propertytype type-house并且类碰巧的顺序不同,则它们将无法匹配.XPath对类没有任何了解,也不了解CSS中使用的空格分隔列表.有关此更多的讨论,参见,例如这个.
您应该使用CSS选择器:
dl.cN-featDetails > dd.propertytype.type-house
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5916 次 |
| 最近记录: |