FAQ
HI friends,

i want to print all the main module links ...but i was getting all the
links printed if take the frame and then extarct all the links using tag
"a".
please correct me..

List<WebElement>
All_Module_Links=driver.findElements(By.xpath("//*[@id='top-menu']"));
WebElement modules_box = All_Module_Links.get(0);
List<WebElement> links =modules_box.findElements(By.tagName("a"));
System.out.println("Total links are : "+links.size());
for(int i=0;i<links.size();i++)
{
System.out.println(links.get(i).getText());

<https://lh4.googleusercontent.com/-2GKPVJCqq9o/UJzjmlu9LFI/AAAAAAAAADQ/lRMX13hI26g/s1600/modules.jpg>
}

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/1fhE1ZiVvNwJ.
To post to this group, send email to webdriver@googlegroups.com.
To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Search Discussions

  • Ram Maram at Nov 12, 2012 at 12:22 pm
    try this below code:

    WebElement all_Links=driver.findElement(
    By.xpath("//*[@id='top-menu']"));
    List<WebElement> mainModule_Links=all_Links.findElements(By.tagName("a"));
    System.out.println("All Links are"+mainModule_Links.size());
    for(int i=0;i<mainModule_Links.size();i++){

    System.out.println( mainModule_Links.get(i).getText());
    }





    Thanks,
    Ram.

    On Fri, Nov 9, 2012 at 4:36 PM, Venkat Suresh wrote:

    HI friends,

    i want to print all the main module links ...but i was getting all the
    links printed if take the frame and then extarct all the links using tag
    "a".
    please correct me..

    List<WebElement>
    All_Module_Links=driver.findElements(By.xpath("//*[@id='top-menu']"));
    WebElement modules_box = All_Module_Links.get(0);
    List<WebElement> links =modules_box.findElements(By.tagName("a"));
    System.out.println("Total links are : "+links.size());
    for(int i=0;i<links.size();i++)
    {
    System.out.println(links.get(i).getText());


    <https://lh4.googleusercontent.com/-2GKPVJCqq9o/UJzjmlu9LFI/AAAAAAAAADQ/lRMX13hI26g/s1600/modules.jpg>
    }

    --
    You received this message because you are subscribed to the Google Groups
    "webdriver" group.
    To view this discussion on the web visit
    https://groups.google.com/d/msg/webdriver/-/1fhE1ZiVvNwJ.
    To post to this group, send email to webdriver@googlegroups.com.
    To unsubscribe from this group, send email to
    webdriver+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/webdriver?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "webdriver" group.
    To post to this group, send email to webdriver@googlegroups.com.
    To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
  • Darrell at Nov 12, 2012 at 2:06 pm
    The concept of "main module links" isn't something I am familiar with but I
    can tell you have the xpath you provided in the first line should get zero
    or one element. There can be only zero or one element with the given ID
    attribute. If the intent is to check to see if there is one element, there
    is nothing in your code to show that. If there is no match and the
    findElements returns zero elements, the second line will throw an exception.

    Personally, I'd code the first line as:

    List<WebElement> topMenuLink = driver.findElements(By.id("top-menu"));
    if(topMenuLink.size() != 1) {
    // handle the error here
    }

    After this, finding all the "main module links" is dependent on the
    structure of the DOM. If all the "main module links" are links which are
    children of the "top-menu" element and they are all anchors then you should
    be able to do something like:

    WebElement modules_box = topMenuLink.get(0);
    List<WebElement> links = modules_box.findElements(By.cssSelector("a"));

    But this assumes all anchor children of the element id="top-menu" will be
    what you want. On the other hand, if you want all the IMMEDIATE children of
    top-menu then why not just do it in one go. If the DOM is:

    <div id="top-menu>
    <a ...>anchor 1</a>
    <a ...>anchor 2</a>
    <a ...>anchor 3</a>
    </div>

    then you can find them using:

    List<WebElement> links =
    driver.findElements(By.cssSelector("div#top-menu>a"));

    Bottom line, it doesn't hurt to have a picture but you really need to
    provide us with a DOM and what you are trying to do. Code which does not
    work and a picture isn't really the information we need.

    Darrell

    On Friday, 9 November 2012 06:06:13 UTC-5, Venkat Suresh wrote:

    HI friends,

    i want to print all the main module links ...but i was getting all the
    links printed if take the frame and then extarct all the links using tag
    "a".
    please correct me..

    List<WebElement>
    All_Module_Links=driver.findElements(By.xpath("//*[@id='top-menu']"));
    WebElement modules_box = All_Module_Links.get(0);
    List<WebElement> links =modules_box.findElements(By.tagName("a"));
    System.out.println("Total links are : "+links.size());
    for(int i=0;i<links.size();i++)
    {
    System.out.println(links.get(i).getText());


    <https://lh4.googleusercontent.com/-2GKPVJCqq9o/UJzjmlu9LFI/AAAAAAAAADQ/lRMX13hI26g/s1600/modules.jpg>
    }
    --
    You received this message because you are subscribed to the Google Groups "webdriver" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/SlEEIOfS53cJ.
    To post to this group, send email to webdriver@googlegroups.com.
    To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupwebdriver @
categorieswatir
postedNov 12, '12 at 10:57a
activeNov 12, '12 at 2:06p
posts3
users3
websitewatir.com

People

Translate

site design / logo © 2021 Grokbase