Single Post

Header

Monday, November 23, 2015

How to fetch all links and click one of those links one by one using selenium webdriver

How to get all the links on a webpage and click the matching website

import java.util.List;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class LinksAllTypesMethods {

@Test
public void seleniumLinkMethods() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(3000);

// Using tag name "a" , we get all the links
List<WebElement> links = driver.findElements(By.tagName("a"));

// Get how many web links are present on a web page
int numberOfWebLinks = links.size();

// Get the text from the links
for (WebElement link : links) {
System.out.println("The link is "+link.getText());
}

// Using for loop
for (int i=0; i<numberOfWebLinks; i++) {
System.out.println(links.get(i).getText());
System.out.println(links.get(i).getAttribute("href"));
}

// Using tag name "a" , we get all the links
List<WebElement> allLinks = driver.findElements(By.tagName("a"));

// Click on a particular link and break it
   for(WebElement singleLink : allLinks) {
    if (singleLink.getText().equals("हिन्दी")) {
    singleLink.click();
    Thread.sleep(3000);
    break;
    }
   }

driver.close();
}
}


Output :
The link is
The link is Gmail
The link is Images
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is
The link is Sign in
The link is ×
The link is Yes, show me
The link is
The link is
The link is
The link is
The link is हिन्दी
The link is বাংলা
The link is తెలుగు
The link is मराठी
The link is தமிழ்
The link is ગુજરાતી
The link is ಕನ್ನಡ
The link is മലയാളം
The link is ਪੰਜਾਬੀ
The link is Privacy
The link is Terms
The link is Settings
The link is
The link is
The link is
The link is
The link is
The link is Advertising
The link is Business
The link is About

https://www.google.co.in/setprefs?suggon=2&prev=https://www.google.co.in/&sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D
Gmail
https://mail.google.com/mail/?tab=wm
Images
https://www.google.co.in/imghp?hl=en&tab=wi&ei=sHV6Vr-PK8eouQTw07jYBw&ved=0EKouCBIoAQ

https://www.google.co.in/intl/en/options/

https://myaccount.google.com/?utm_source=OGB

https://www.google.co.in/webhp?tab=ww&ei=sHV6Vr-PK8eouQTw07jYBw&ved=0EKkuCAEoAQ

https://maps.google.co.in/maps?hl=en&tab=wl

https://www.youtube.com/?gl=IN

https://play.google.com/?hl=en&tab=w8

https://news.google.co.in/nwshp?hl=en&tab=wn&ei=sHV6Vr-PK8eouQTw07jYBw&ved=0EKkuCAUoBQ

https://mail.google.com/mail/?tab=wm

https://drive.google.com/?tab=wo

https://www.google.com/calendar?tab=wc

https://plus.google.com/?gpsrc=ogpy0&tab=wX

https://translate.google.co.in/?hl=en&tab=wT

https://photos.google.com/?tab=wq

https://www.google.co.in/intl/en/options/

https://docs.google.com/document/?usp=docs_alc

https://books.google.co.in/bkshp?hl=en&tab=wp&ei=sHV6Vr-PK8eouQTw07jYBw&ved=0EKkuCA0oDQ

https://www.blogger.com/?tab=wj

https://www.google.com/contacts/?hl=en&tab=wC

https://www.google.co.in/intl/en/options/
Sign in
https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.co.in/
×
javascript:void(0)
Yes, show me
https://www.google.com/url?q=https://www.google.com/homepage/hp-firefox.html%3Futm_source%3Dgoogle.com%26utm_medium%3Dcallout%26utm_campaign%3DFFDHP&source=hpp&id=5082229&ct=7&usg=AFQjCNGpJYJLVlrP4aS5wSlNy1fQs3_xmg

https://www.google.co.in/webhp?hl=en

https://support.google.com/websearch/answer/186645?hl=en-IN

https://www.google.co.in/search?site=&q=%27Tis+the+season!&oi=ddle&ct=holidays-2015-day-1-6575248619077632-hp&hl=en&sa=X&ved=0ahUKEwjoy72y4fHJAhXMC44KHVivBi4QNggD

javascript:void(0);
हिन्दी
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=hi&source=homepage
বাংলা
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=bn&source=homepage
తెలుగు
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=te&source=homepage
मराठी
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=mr&source=homepage
தமிழ்
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=ta&source=homepage
ગુજરાતી
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=gu&source=homepage
ಕನ್ನಡ
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=kn&source=homepage
മലയാളം
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=ml&source=homepage
ਪੰਜਾਬੀ
https://www.google.co.in/setprefs?sig=0_Qa6VBRR6zqKEKCiglQ9kpgson64%3D&hl=pa&source=homepage
Privacy
https://www.google.co.in/intl/en/policies/privacy/?fg=1
Terms
https://www.google.co.in/intl/en/policies/terms/?fg=1
Settings
https://www.google.co.in/preferences?hl=en

https://www.google.co.in/preferences?hl=en-IN&fg=1

https://www.google.co.in/advanced_search?hl=en-IN&fg=1

https://www.google.co.in/history/optout?hl=en-IN&fg=1

https://support.google.com/websearch/?p=ws_results_help&hl=en-IN&fg=1

javascript:void(0)
Advertising
https://www.google.co.in/intl/en/ads/?fg=1
Business
https://www.google.co.in/services/?fg=1
About
https://www.google.co.in/intl/en/about.html?fg=1


38 comments:

  1. Good post.know about How to fetch all links and click one of those links one by one using selenium webdriver.Thanks for sharing this valuable information. best selenium training in chennai
    best selenium training institute in chennai
    Training with placement in chennai

    ReplyDelete
  2. Inspiring quest there. What happened after? Thanks!

    ReplyDelete
  3. Hello there, just became alert to your blog through Google, and found that it's truly informative. I'm gonna watch out for brussels. I will appreciate if you continue this in future. Numerous people will be benefited from your writing. Cheers!

    ReplyDelete
  4. whoah this blog is magnificent i really like reading your posts. Stay up the great work! You recognize, a lot of persons are searching round for this information, you can help them greatly.

    ReplyDelete
  5. Article writing is also a excitement, if you be acquainted with then you can write otherwise it is complex to write.

    ReplyDelete
  6. I want to to thank you for this excellent read!! I certainly loved every bit of it. I have you saved as a favorite to look at new stuff you post…

    ReplyDelete
  7. Helpful info. Lucky me I discovered your web site by chance, and I am stunned why this twist of fate didn't happened earlier! I bookmarked it.

    ReplyDelete
  8. May I simply say what a relief to find an individual who genuinely knows what they are talking about on the net. You certainly understand how to bring an issue to light and make it important. More and more people really need to read this and understand this side of your story. I can't believe you are not more popular since you surely possess the gift.

    ReplyDelete
  9. This excellent website definitely has all of the info I wanted about this subject and didn't know who to ask.

    ReplyDelete
  10. Hello there, just became aware of your blog through Google, and found that it is really informative. I'm going to watch out for brussels. I will be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers!

    ReplyDelete
  11. This blog was... how do you say it? Relevant!! Finally I have found something which helped me. Thanks!

    ReplyDelete
  12. What's up, always i used to check weblog posts here early in the morning, as i enjoy to gain knowledge of more and more.

    ReplyDelete
  13. Hmm is anyone else encountering problems with the pictures on this blog loading? I'm trying to find out if its a problem on my end or if it's the blog. Any feedback would be greatly appreciated.

    ReplyDelete
  14. Hi, its nice article regarding media print, we all know media is a great source of information.

    ReplyDelete
  15. What's up, I desire to subscribe for this webpage to take hottest updates, therefore where can i do it please help out.

    ReplyDelete
  16. I am in fact thankful to the holder of this web page who has shared this enormous article at here.

    ReplyDelete
  17. This blog was... how do I say it? Relevant!! Finally I've found something that helped me. Appreciate it!

    ReplyDelete
  18. You can definitely see your skills within the article you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. At all times follow your heart.

    ReplyDelete
  19. With havin so much written content do you ever run into any issues of plagorism or copyright infringement? My site has a lot of exclusive content I've either written myself or outsourced but it seems a lot of it is popping it up all over the internet without my agreement. Do you know any solutions to help prevent content from being stolen? I'd certainly appreciate it.

    ReplyDelete
  20. My partner and I stumbled over here coming from a different page and thought I might as well check things out. I like what I see so now i am following you. Look forward to checking out your web page repeatedly.

    ReplyDelete
  21. Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but instead of that, this is great blog. A great read. I'll certainly be back.

    ReplyDelete
  22. Hi there everyone, it's my first visit at this site, and post is truly fruitful in favor of me, keep up posting such articles or reviews.

    ReplyDelete
  23. I was suggested this web site by my cousin. I'm not sure whether this post is written by him as no one else know such detailed about my difficulty. You are wonderful! Thanks!

    ReplyDelete
  24. I do trust all the ideas you've introduced to your post. They are really convincing and can certainly work. Still, the posts are very short for starters. May you please prolong them a little from subsequent time? Thank you for the post.

    ReplyDelete
  25. Hey There. I found your weblog using msn. This is a really neatly written article. I'll make sure to bookmark it and return to read extra of your helpful info. Thank you for the post. I'll definitely return.

    ReplyDelete
  26. It's a shame you don't have a donate button! I'd certainly donate to this fantastic blog! I guess for now i'll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will talk about this site with my Facebook group. Talk soon!

    ReplyDelete
  27. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?

    ReplyDelete
  28. Hey There. I found your blog using msn. This is a really smartly written article. I'll make sure to bookmark it and come back to learn extra of your helpful info. Thanks for the post. I will certainly return.

    ReplyDelete
  29. It is really a great and helpful piece of info. I am glad that you simply shared this useful info with us. Please stay us up to date like this. Thanks for sharing.

    ReplyDelete
  30. When I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I recieve four emails with the exact same comment. Perhaps there is an easy method you can remove me from that service? Appreciate it!

    ReplyDelete
  31. Hi there! This is my first comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading your articles. Can you recommend any other blogs/websites/forums that deal with the same topics? Thanks a lot!

    ReplyDelete
  32. For hottest news you have to pay a visit web and on the web I found this web page as a most excellent web site for most up-to-date updates.

    ReplyDelete
  33. Hey, I think your blog might be having browser compatibility issues. When I look at your website in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, awesome blog!

    ReplyDelete
  34. I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz answer back as I'm looking to create my own blog and would like to know where u got this from. many thanks

    ReplyDelete
  35. Excellent, what a website it is! This blog provides helpful facts to us, keep it up.

    ReplyDelete
  36. Quality posts is the key to attract the visitors to pay a visit the site, that's what this web site is providing.

    ReplyDelete