George Smith
George Smith

Reputation: 299

XPath related questions to locate elements in Webdriver 2 Java

Dear Selenium Webdriver 2 Experts,

I am new to this framework and need your advice on some XPath related questions on the following webpage XHTML snippet:

   <dl class="cN-featDetails">
        <dt class="propertytype">Property type</dt>
        <!-- line 3 --> <dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl04_lstTemplate_ddPropertyType" class="propertytype type-house" title="Property type: House">House</dd>
       <!-- line 3a --> <!-- or  class="propertytype type-townhouse"--->
        .......
<div class="main-wrap">
    <div class="s-prodDetails">
        <a id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl04_lstTemplate_hypMainThumb" class="photo contain" href="/Property/For-Sale/House/LA/St Gabriel/?adid=2009938763">img id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl04_lstTemplate_imgMainThumb" title="44 Crown Street, St Gabriel" src="http://images.abc.com/img/2012814/2778/2009938763_1_PM.JPG?mod=121010-210000" alt="Main photo of 44 Crown Street, St Gabriel - More Details" style="border-width:0px;" /></a>
        <div class="description">
             <h4><span id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl04_lstTemplate_lblPrice">Offers Over $900,000 </span></h4>
             <h5>SOLD BY WAISE YUSOFZAI</h5><p>CHARACTER FAMILY HOME... Filled with warmth and charm, is a very well maintained family home in a quiet level street. Be...</p>
    </div>                                                                                                                                                                                                                                                                                                                        
    <a id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl04_lstTemplate_hypMoreDetails" class="button" href="/Property/For-Sale/House/LA/St Gabriel/?adid=2009938763">More Details</a>
    <dl id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl04_lstTemplate_dlAgent" class="agent">
        <!-- line 19 --> <dt>Advertiser</dt>
        <!-- line 20 --> <dd class="contain">                                       
        <!-- line 20a --> <!-- or class="" -->
            <img id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl04_lstTemplate_imgAgencyLogo" title="Carmen Jones Realty" src="http://images.abc.com/img/Agencys/2778/searchlogo_2778.GIF" style="border-width:0px;" />                                                                                                                                                                                                                                                     
        </dd>
    </dl>
</div>

( a ) How to test whether an element exist or not? e.g. either line 3 or 3a exist but not both. The findElement() method will cause an exception which is what I am trying avoid. Another option is use findElements() before checking whether its collection list result is empty or not. This approach seems to be a long winded way of doing it. Are there any other simpler way to validate the existence of an element without the risk of causing an exception? The following statements did not work or cause exception:

WebElement resultsDiv = driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[1]/h1/em"));
// If results have been returned, the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {
    break;
}

( b ) Is there a simple way to validate the existence of either of the elements by incorporating boolean operator, and regex, as part of the findElement() or findElements()? This would significantly reduce the number of finds as well as simplifying the search.

( c ) Is it possible to use XPath syntax when searching Element by TagName. e.g. driver.findElement(By.tagName("/div[@class='report']/result"));

( d ) Is it possible to use regex in XPath search such as driver.findElement(By.xpath("//div[@class='main-wrap']/dl[@class='agent']/dd[@class='' OR @class='contain']")) for line 20 - 20a?

( e ) How to reference the immediate following node? e.g. Assuming the current node is

Advertiser
on line 19, how to lookup title of which is under , where its class name can have a value of "contain" or nothing "". There could potentially be multiple tags within on line 18.

I have use XPath on XML document in the past but would like to expand the ability to locate elements within Webdriver 2.

Any assistance would be very greatful.

Thanks a lot,

Jack

Upvotes: 2

Views: 6580

Answers (2)

eugene.polschikov
eugene.polschikov

Reputation: 7339

considering the answer given by Zarkonnen, i'd add to (a) point

 public boolean isElementPresent(By selector)
       {
           return driver.findElements(selector).size()>0;
       }

this method i use for verifying that element is present on the page.

you can also involve isDisplayed() method that can work in pair with isElement is present:

driver.findElement(By locator).isDisplayed()

Returning back to your source: Suppose we want to verify wheter 'More details' links is present on the page or not. More Details link

String cssMoreDetails = "a[id='ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl04_lstTemplate_hypMoreDetails']";
//and you simply call the described above method
isElementPresent(By.cssSelector(cssMoreDetails));
//if you found xPath then it be
isElementPresent(By.xpath("//*..."));

And always try to verify found web elements' locators in firepath

verify

In that way you can get xPath of the elment automatically. See the screen attached. xPath generation

Upvotes: 3

Zarkonnen
Zarkonnen

Reputation: 22478

(a) & (d): You can use the | XPath operator to specify two alternate paths in the same expression.

(c): You can't use XPath in byTagName, but you can use // to look for any descendant of a given node.

(e): You can use XPath axes like parent and following-sibling to select all nodes relative to a given node.

I hope these pointers are helpful. I also recommend the excellent Selenium Locator Rosetta Stone (PDF) as a reference on how to construct XPaths.

Upvotes: 1

Related Questions