1

I want xpath to select the seat "12A" from the web page which has list of seats to select and its source code is as follows:

<div class="seats" style="top: 48px;">
  <ul class="row_12 leftOfAisle">
    <li><a class="" data-row="12" data-seat="A" data-code="SPST" data-amount="300.00" href="javascript:;">
      <span>12A</span></a></li>
    <li><a class="" data-row="12" data-seat="B" data-code="SPST" data-amount="300.00" href="javascript:;">
      <span>12B</span></a></li>
    <li><a class="" data-row="12" data-seat="C" data-code="SPST" data-amount="300.00" href="javascript:;">
      <span>12C</span></a></li>
  </ul>
</div>

I have tried with many option and it didn't worked. i was ended with this xpath:

"//*[@class='seats']/ul[@class='row_12 leftOf`Aisle']/li[1]/a"
drkthng
  • 6,651
  • 7
  • 33
  • 53
thiru
  • 13
  • 1
  • 3
  • in Chrome's development tools `Elements` tab you can select any element, right click and select `Copy XPath`. – xZ6a33YaYEfmv Oct 09 '15 at 07:15
  • what is your requirement? you want to return 12A, but what do you know before your search? I ask because it would be no problem to search for the span Element that contains "12A" but I think that might not be your real requirement. Do you need the span or the anchor element or the li element? – drkthng Oct 09 '15 at 07:24

1 Answers1

1

Use below Xpath

//div[@class='seats']/ul[@class='row_12 leftOfAisle']//span[contains(.,'12A')]

Below is the code for same:-

List<WebElement> allprice = driver.findElements(By.xpath("//div[@class='seats']/ul[@class='row_12 leftOfAisle']//span"));
for(WebElement price: allprice){

        System.out.println(price.getText());
        price.click() //If you want to click them one by one

}
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • It is good. But how to get the list of all the elements inside the ul. I need to store the elements in a list. – thiru Oct 09 '15 at 09:54
  • Thanks. the actual problem is under div[class = 'seats'], there are 25 unordered lists (ul) and inside each ul, there is a list with 3 elements, i want to store all the elements into a list. – thiru Oct 09 '15 at 11:09
  • In that case use this xpath //div[@class='seats']/ul//span .. It will return all the elements present in all ul tags... Thanks for accepting the answer, please feel free to vote up. It will really help :) – Shubham Jain Oct 09 '15 at 11:19