Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

xpath - Is string matches() supported in Selenium Webdriver 2?

Dear Selenium Webdriver Experts,

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

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: "

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?

This code snippet is looking up this URL.

I am using Selenium 2.25.0, Java 1.7.0_11 on Windows XP & 7 platforms.

Your advice would be much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unfortunately, the matches() function is a part of XPath 2.0.

WebDriver uses the Wicked Good XPath library that only supports XPath 1.0.

Therefore, your XPath expression is illegal and you should rewrite it to only use features and functions from XPath 1.0.

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.

You should really use a CSS selector instead:

dl.cN-featDetails > dd.propertytype.type-house

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...