PavanS
PavanS

Reputation: 327

How to write a runtime XPATH query in selenium web driver C #

I was trying to find the XPath for the span element inside the div element source code :

<div class="ohg-patient-banner-suppl-info-section-detail" aria-label="" aria-hidden="false">

    <div class="ohg-patient-banner-suppl-info-custom-field">
        <span class=" " aria-hidden="true"></span>
        
            <span class=" ohp-metadata-labe">TestLocation</span>
        
        <span class="ohg-patient-banner-suppl-info-custom-row-value-icon  " aria-hidden="true"></span>
        <span class=" ohg-patient-banner-suppl-info-value">TestLocation Value - </span>
    </div>

    <div class="ohg-patient-banner-suppl-info-custom-field">
        <span class=" " aria-hidden="true"></span>
        
            <span class=" ohp-metadata-label">TestArea</span>
        
        <span class="ohg-patient-banner-suppl-info-custom-row-value-icon fa fa-user-md " aria-hidden="true"></span>
        <span class=" ohg-patient-banner-suppl-info-value">TestArea Value</span>
    </div>

    <div class="ohg-patient-banner-suppl-info-custom-field">
        <span class=" " aria-hidden="true"></span>
        
            <span class=" ohp-metadata-label">Testnet</span>
        
        <span class="ohg-patient-banner-suppl-info-custom-row-value-icon  " aria-hidden="true"></span>
        <span class=" ohg-patient-banner-suppl-info-value">Testnet value</span>
    </div>

MY Code to find XPath: when i wrote the below code i only gives me one span element text which TestLocation in the first div.

lblBannerVisitNames.Clear();
lblBannerVisitValues.Clear();
IList<IWebElement> VisitFieldsBanner = driver.FindElements(lblAllvisitinfoExpandedMapping);
int NumberofVisitFieldsBanner = VisitFieldsBanner.Count;

for (int i =1; i <= NumberofVisitFieldsBanner; i++)
{
    //because we are looping through the list , the xpath query is built at runtime with value i 
    lblBannerVisitNames.Add(driver.FindElement(By.XPath(".//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class='ohp-metadata-label']")).Text);

    if (driver.FindElements(By.XPath(".//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class=' ohg-patient-banner-suppl-info-value']")).Count > 0)
        lblCurrentVisitFieldValues.Add(driver.FindElement(By.XPath(".//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class=' ohg-patient-banner-suppl-info-value']")).Text);

    else
        lblCurrentVisitFieldValues.Add(ClinicalPortalConstants.NOT_AVAILABLE);

    //Add debgugging Later 
}

Upvotes: 0

Views: 202

Answers (1)

PavanS
PavanS

Reputation: 327

I changed my XPATH where i forgot to use the "i" variable value

lblBannerVisitNames.Add(driver.FindElement(By.XPath(".//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field'][" + i + "]/span[@class=' ohp-metadata-label']")).Text);

Upvotes: 0

Related Questions