简体   繁体   中英

XPath questions for Selenium Webdriver C# to locate search result on webpage

I am new to Selenium and trying to implement Webjet (www.webjet.com.au) to complete a search process and a flight booking after the search result. However I had difficulties while locating search results , I inspected the link in firebug however it is wrapped in div/ and confused me. I tried Linked Text too but it wouldn't work.

Edit: The element I am trying to locate is the flight fare (eg:$97),The html code from Firebug is shown below:

<div class="span1 matrix-price multifare">
<div>
<span class=" without-baggage baggage fare-select OneWay Morning oneway" data-price="97" data-flight-group-no="216" data-flight-halfreturn-token="" data-fare-id="1000072" data-baggage-id="JQ.BGNO" data-original-title="">$97</span>

The XPath for this element is .// [@id='flight-matrixes-wrapper']/div[1]/div/div[2]/div[1]/div[10]/div[3]/div[1]/span Different results will be generated depends on the flight search criteria, I looked up from previous examples and tried to use driver.FindElement(By.XPath("// [contains(text(),'$')]/descendant::*)")).Click(); but not very sure.

I tried a search on the given web site and based on that I think following xpath might be helpful

//div[contains(@class,'matrix-price')]//div//span[text()='$328']

If you know the expected price(here $328) you can directly use above xpath , or else you could use below to get all pricing shows on the grid

//div[contains(@class,'matrix-price')]//div//span[contains(text(),'$')]

Anyway if you are going to click a specific price you need to know the exact price you want to click

Additional Notes

01) Above xpaths are given assuming you are refering to the pricing grid similar to the attached image!

在此处输入图片说明

02) Make sure that you are waiting enough until the grid is properly populated

03) I have used Fire Finder to build the xpath, fire finder highlights elements matches with given xpath (as shown in the attached image as well )

Code Snippet for Waiting

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement Elem = wait.Until<IWebElement>((d) =>
{
    return d.FindElement(By.Class("matrix-price"));
});

You could wait for the exact Pricing element instead of above . You could increases the time span(in above code) if 10 seconds not enough

If you want to clich here:

Try:

 driver.FindElement(By.XPath(".//*[@class='span1 matrix-price multifare']")).Click();

If you want to clich here: $97

Try:

 driver.FindElement(By.XPath(".//*[@class=' without-baggage baggage fare-select OneWay Morning oneway']")).Click();

I only know this, because i'm new in this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM