3

I wanted to get the names of all the Travels name, present in the tags, ( They are dynamic so wanted to take in array to have count) in http://www.redbus.in/Booking/SelectBus.aspx?fromCityId=124&fromCityName=Hyderabad&toCityId=462&toCityName=Mumbai&doj=30-May-2013&busType=Any&opId=0

here's the some part of html file

<div id="t1-results">
<table id="t1-list5" class="list5" cellspacing="0" cellpadding="0" style="margin-top:0px;">
 <tbody id="t1-0">
 <tr class="r1">
 <td class="i1">Akbar Travels</td>
 <td class="i2">
 <td class="i3">
 <td class="i4">
 <td class="i5">
 <td class="i6">
 <td class="i7">
  </tr>
 <tr class="r2">
 <tr class="r3">
 </tbody>
 <tbody class="hrblank">
 <tbody id="t1-1" class="G">
 <tr class="r1">
 <td class="i1">Omer Travels</td>
 <td class="i2">
 <td class="i3">
 <td class="i4">
 <td class="i5">
 <td class="i6">
 <td class="i7">
 </tr>
 <tr class="r2">
 <tr class="r3">
 </tbody>
 <tbody class="hrblank">
 <tbody id="t1-2">
 <tr class="r1">
 <td class="i1">Sangita Travel Agency</td>
 <td class="i2">
 <td class="i3">
 <td class="i4">
 <td class="i5">
 <td class="i6">
 <td class="i7">
 </tr>

I have tried with

        List<WebElement> linkElements = driver.findElements(By.tagName("//table[@cellspacing=\"0\"]"));

        String[] linkTexts = new String[linkElements.size()];

when i debug got as linkElements ArrayList<E> (id=82)
linkTexts String[0] (id=95)

Not working.

tried this also

List<WebElement> linkElements = driver.findElements(By.tagName("t1-results"));

        String[] linkTexts = new String[linkElements.size()];

Can anyone please suggest me how to take the names.

Learner
  • 945
  • 11
  • 23
  • 38
  • I believe your problem is not with identifying the elements; what you need is to iterate over the returned list and extract the string from each one. See the answer by Marlon Bernardes below for some code illustrating how to iterate over the list. – Vince Bowdren May 30 '13 at 13:18

4 Answers4

2

Try using a CSS selector, like this:

List<WebElement> linkElements = driver.findElements(By.cssSelector("table tr.r1 > td.i1")); 

I just made a quick test and this code finds all the 63 matches (as of this writing):

driver.get("http://www.redbus.in/Booking/SelectBus.aspx?fromCityId=124&fromCityName=Hyderabad&toCityId=462&toCityName=Mumbai&doj=30-May-2013&busType=Any&opId=0");

//Find's the elements using a CSS selector: all td's (with class "i1"), directly inside a tr (with class r1) which are inside a table.
List<WebElement> linkElements = driver.findElements(By.cssSelector("table tr.r1 > td.i1"));
String[] linksText = new String[linkElements.size()];
int index = 0;
for(WebElement element : linkElements){
    linksText[index++] = element.getText();
}
Marlon Bernardes
  • 13,265
  • 7
  • 37
  • 44
0

Xpath is also good option, use this hope help you.

List<WebElement> linkElements = driver.findElements(By.Xpath("Your Xpath"));
String[] linksText = new String[linkElements.size()];
int index = 0;
for(WebElement element : linkElements){  
linksText[index++] = element.getText();
}
Chetan
  • 2,360
  • 3
  • 18
  • 33
  • Xpath selectors are often brittle: slight changes to DOM might cause your selector not to work as expected or not to work at all. The preferred selector order is: Id/name > css > xpath. – Marlon Bernardes May 29 '13 at 05:11
  • Actually i spending lots of time with Xpath & no though idea of CSS -selector, look at following link http://ejohn.org/blog/xpath-css-selectors/ John clearly mension that, "The biggest thing to realize is that CSS Selectors are, typically, very short – but woefully underpowered, when compared to XPath". – Chetan May 29 '13 at 06:12
  • Selenium recomends not to use XPath, since browsers that don't implement Xpath capabilities might have strange issues. Check these links: http://docs.seleniumhq.org/docs/03_webdriver.jsp#by-xpath and http://stackoverflow.com/questions/13975595/why-one-should-prefer-using-css-over-xpath-in-ie – Marlon Bernardes May 29 '13 at 13:20
0

Try with the CSS Count Concept given below

int size=driver.findElements(By.cssSelector("#t1-results > #t1-list5 > tbody > tr.r1")).size();
for(int i=1;i<=size();i++)
{
    String travelname = driver.findElement(By.cssSelector("#t1-results > #t1-list5 > tbody > tr.r1 > td.i1"))
}

Let me know is the above script works for you.

Csq
  • 5,775
  • 6
  • 26
  • 39
user3487861
  • 340
  • 2
  • 2
0

Both CSS and XPath entries can be used for problems such as these, but some clarification needs to be made:

  1. some CSS entries are relevant to frequently-changed style features and should not be used as locators,
  2. characterizing XPath locators en-masse as "fragile" hearkens back to the olde days when the term implied the full DOM path between the parent /html and the end widget being sought.

There are algorithms for constructing XPath locators that are designed first and foremost to be robust in light of frequent/massive DOM retooling -- such locators are anything but fragile.

The last concern noted earlier (that some browsers [notably IE] have non-standard XPath implementations) is unfortunately true, irrespective of your take on the value of XPaths in general.

That isn't grounds for abandoning XPath as a tool for one's toolbag -- but it might lend credence to abandoning a browser with so many proprietary features.

Qiu
  • 5,651
  • 10
  • 49
  • 56