问题描述
Selenium Webdriver专家,
Dear Selenium Webdriver Experts,
我想知道Selenium Webdriver中的字符串匹配方法是否与Java中的以下代码段一起正常工作:
I am wondering whether the string matches method in Selenium Webdriver is working properly with the following code snippet in Java:
if (property.findElements(By.xpath("./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house']")).size() > 0 ) { // line 229
下面是第229行从以下位置读取的xhtml网页:
Below is the xhtml webpage where line 229 is reading from:
<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>
但是,这导致了以下错误:
However, this resulted in the following error:
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: "
我也尝试了matches(class,'propertytype.*$']")
,也没有成功.
I have also tried matches(class,'propertytype.*$']")
without success either.
类的名称会根据属性是房屋(类型房屋)还是公寓(类型公寓)而变化....
The name of class changes depending on whether the property is a house (type-house) or apartment (type-apartment)…..
关于如何在匹配项中使用正则表达式以检查此属性类型元素中是否存在值/有效树节点的任何建议?
Any suggestion on how to use regex in matches to check whether there is value / valid tree node in this property type element?
此代码段正在查找这个网址.
我在Windows XP&上使用Selenium 2.25.0,Java 1.7.0_11 7个平台.
I am using Selenium 2.25.0, Java 1.7.0_11 on Windows XP & 7 platforms.
您的建议将不胜感激.
推荐答案
不幸的是, matches()
函数是XPath 2.0的一部分.
Unfortunately, the matches()
function is a part of XPath 2.0.
WebDriver使用仅支持XPath 1.0的 Wicked Good XPath 库
WebDriver uses the Wicked Good XPath library that only supports XPath 1.0.
因此,您的XPath表达式是非法的,应该重写它以仅使用 XPath中的功能部件1.0 .
Therefore, your XPath expression is illegal and you should rewrite it to only use features and functions from XPath 1.0.
我认为您可以简单地将matches()
调用替换为 c3> .就是说,不是被认为是通过contains()
匹配类名的好习惯,因为type-house
也将匹配type-houses
.同样,如果您匹配propertytype type-house
并且类碰巧以不同的顺序排列,则它们将不会被匹配. XPath对类一无所知,对CSS中使用空格分隔的列表一无所知.有关此问题的更多讨论,请参见此.
I think you could simply replace the matches()
call in your examply with contains()
. That said, it's not considered a good practise to match class names via contains()
, because type-house
would also match type-houses
. Also if you match for propertytype type-house
and the classes happen to be in different order, they won't get matched. XPath doesn't know anything about classes nor about space-separated lists used in CSS. For more discussion on this, see e.g. this.
您应该真正使用CSS选择器:
You should really use a CSS selector instead:
dl.cN-featDetails > dd.propertytype.type-house
这篇关于Selenium Webdriver 2支持字符串match()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!