Wednesday, December 24, 2014

Extract all web links from Website using selenium Webdriver



This is very important interview question.

In this example
  1. Navigating to http://automationplace.blogspot.in/
  2. Getting all the webelement with tagname "a"
  3. Store it in list i.e)List<Webelement>
  4. loop to get the attribute with name href i.e)getAttribute("href")

Sourcecode :


import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class countlinks {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://automationplace.blogspot.in/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List<WebElement> list = driver.findElements(By.tagName("a"));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getAttribute("href"));
}
driver.close();
}
}

After run this code i get all the links from my Webpage




Finally my output look like this :

http://automationplace.blogspot.in/2014/12/difference-between-webdriverget-and.html
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfgn4m04aCy0GQBYiQ3Dhl55jMvvuaDKWPvQ6D_SI7zM_Hj6FPaPC_K0W9kTYfPXIw96Yyl7qglaCCGLjTDrJwW9VO779KQQ757wqQqb0hCK8SqPVCS3pa-pFF5aiFTu2QbQ-Mbow94bg/s1600/images+(1).jpg
http://seleniumeasy.com/selenium-tutorials/navigation-methods-in-webdriver-with-examples
http://seleniumguru.blogspot.in/
http://seleniumeasy.com/selenium-tutorials/navigation-methods-in-webdriver-with-examples
http://seleniumguru.blogspot.in/
http://seleniumguru.blogspot.in/
http://www.example.com/
http://www.google.com/
http://www.blogger.com/profile/15375547504465355484
http://automationplace.blogspot.in/2014/12/difference-between-webdriverget-and.html
http://automationplace.blogspot.in/2014/12/difference-between-webdriverget-and.html#comment-form
http://www.blogger.com/post-edit.g?blogID=7649703008756780120&postID=3034077643133971642&from=pencil
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=3034077643133971642&target=email
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=3034077643133971642&target=blog
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=3034077643133971642&target=twitter
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=3034077643133971642&target=facebook
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=3034077643133971642&target=pinterest
null
http://automationplace.blogspot.in/2014/12/verify-image-in-webpage-using-selenium.html
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjtj9IjZPF17u4Y8wpk11WsnyvZnbCfTnC3A48JpNStIt6_b3x6v8Yqhk-fm-Y9_w2JldweNgMLkqZEEXJ9l7zMWdvQG9utFeCPEZqDi-iFo4yQGiG885N9_8Ufx7FZdpC2c3V36CnEYg/s1600/test-concept.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkWak_ilW92iM7Qv2DIBmccd-NN1SooNATrPP8PnyJEZJ0dQ-wuo87vAXotuKzxTPd2ARdOK3IU4_dCkZ4-34R0DLfGO9hsD84DHG_WrbquKcltIJ6u29kfzCakoSxkCRfyt8Z4G-5e-I/s1600/verify+image.png
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement
http://www.blogger.com/profile/15375547504465355484
http://automationplace.blogspot.in/2014/12/verify-image-in-webpage-using-selenium.html
http://automationplace.blogspot.in/2014/12/verify-image-in-webpage-using-selenium.html#comment-form
http://www.blogger.com/post-edit.g?blogID=7649703008756780120&postID=5784969822933189169&from=pencil
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=5784969822933189169&target=email
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=5784969822933189169&target=blog
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=5784969822933189169&target=twitter
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=5784969822933189169&target=facebook
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=5784969822933189169&target=pinterest
null
http://automationplace.blogspot.in/2014/12/we-going-to-perform-data-driven-using.html
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioNpavERZ7ppvByggoUw09w8B88MK6p7HyYbhR06OUhA7Qhiqf3tLPArVzm6z5boHHi84cbdLRybjvn0PgYNuaieoxuwb8OUp5XUmPpoYRlBTukVeeJUvfILXhk_ZQY5IKUSzn-yBrs-E/s1600/result.png
http://helloselenium.blogspot.com/2014/01/what-is-apache-poi-library.html
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvHgDNBJ9bZUUYB1qiCZOe5Enp1Zf3kycoFv7SOOKoU0jWDaXUixMSc56sYIf-DZLyYZjMvBKca8ok9f8AUyWIpjhi9hpU6M-8lHKNPc0w2-fQ_T7YJ0bvuCGu9J7bgkoPENGeqJ2cV_Y/s1600/excel01.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkfqWVy8PtGIm1LRP9iSr9itsMBNOn3eG73lWu6G-ipv0Hg0znLAPDZqqhW03nkYlwy9uyAQ6C6vnaGRXT4TaC-U2hf9qHLUIS00sche16NYZ0v36kvRtzqAN8WGhgrfPLYW6Sw3F4Kkc/s1600/excel.png
http://www.blogger.com/profile/15375547504465355484
http://automationplace.blogspot.in/2014/12/we-going-to-perform-data-driven-using.html
http://automationplace.blogspot.in/2014/12/we-going-to-perform-data-driven-using.html#comment-form
http://www.blogger.com/post-edit.g?blogID=7649703008756780120&postID=4152045802400071753&from=pencil
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=4152045802400071753&target=email
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=4152045802400071753&target=blog
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=4152045802400071753&target=twitter
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=4152045802400071753&target=facebook
http://www.blogger.com/share-post.g?blogID=7649703008756780120&postID=4152045802400071753&target=pinterest
http://automationplace.blogspot.in/search?updated-max=2014-12-23T06:22:00-08:00&max-results=7
http://automationplace.blogspot.in/
http://automationplace.blogspot.com/feeds/posts/default
http://www.blogger.com/profile/15375547504465355484
http://www.blogger.com/profile/15375547504465355484
http://www.blogger.com/profile/15375547504465355484
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=Profile&widgetId=Profile1&action=editWidget&sectionId=sidebar-right-1
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=HTML&widgetId=HTML2&action=editWidget&sectionId=sidebar-right-1
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=Stats&widgetId=Stats1&action=editWidget&sectionId=sidebar-right-1
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=CustomSearch&widgetId=CustomSearch1&action=editWidget&sectionId=sidebar-right-1
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=Followers&widgetId=Followers1&action=editWidget&sectionId=sidebar-right-1
javascript:void(0)
http://automationplace.blogspot.in/search?updated-min=2014-01-01T00:00:00-08:00&updated-max=2015-01-01T00:00:00-08:00&max-results=17
javascript:void(0)
http://automationplace.blogspot.in/2014_12_01_archive.html
http://automationplace.blogspot.in/2014/12/difference-between-webdriverget-and.html
http://automationplace.blogspot.in/2014/12/verify-image-in-webpage-using-selenium.html
http://automationplace.blogspot.in/2014/12/we-going-to-perform-data-driven-using.html
http://automationplace.blogspot.in/2014/12/selenium-webdriver-automation-logger.html
http://automationplace.blogspot.in/2014/12/difference-between-assert-and-verify.html
http://automationplace.blogspot.in/2014/12/read-xml-file-trigger-selenium-webdriver.html
http://automationplace.blogspot.in/2014/12/save-webpage-dropdown-value-in-notepad.html
http://automationplace.blogspot.in/2014/12/data-driven-test-case-failure-take.html
http://automationplace.blogspot.in/2014/12/selenium-web-automation-tool.html
http://automationplace.blogspot.in/2014/12/how-to-check-if-text-is-present-or-not.html
http://automationplace.blogspot.in/2014/12/object-repository-in-selenium-using.html
http://automationplace.blogspot.in/2014/12/identifying-elements-in-selenium.html
http://automationplace.blogspot.in/2014/12/top-5-unit-testing-toolsframework.html
http://automationplace.blogspot.in/2014/12/data-driven-using-testng.html
http://automationplace.blogspot.in/2014/12/keyword-driven-framework.html
http://automationplace.blogspot.in/2014/12/automation-frameworks.html
http://automationplace.blogspot.in/2014/12/selenium-webdriver-architecture.html
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=BlogArchive&widgetId=BlogArchive1&action=editWidget&sectionId=sidebar-right-1
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=HTML&widgetId=HTML1&action=editWidget&sectionId=sidebar-right-1
http://automationplace.blogspot.in/2014/12/data-driven-using-testng.html
http://automationplace.blogspot.in/2014/12/data-driven-using-testng.html
http://automationplace.blogspot.in/2014/12/object-repository-in-selenium-using.html
http://automationplace.blogspot.in/2014/12/object-repository-in-selenium-using.html
http://automationplace.blogspot.in/2014/12/top-5-unit-testing-toolsframework.html
http://automationplace.blogspot.in/2014/12/top-5-unit-testing-toolsframework.html
http://automationplace.blogspot.in/2014/12/identifying-elements-in-selenium.html
http://automationplace.blogspot.in/2014/12/identifying-elements-in-selenium.html
http://automationplace.blogspot.in/2014/12/automation-frameworks.html
http://automationplace.blogspot.in/2014/12/automation-frameworks.html
http://automationplace.blogspot.in/2014/12/we-going-to-perform-data-driven-using.html
http://automationplace.blogspot.in/2014/12/we-going-to-perform-data-driven-using.html
http://automationplace.blogspot.in/2014/12/how-to-check-if-text-is-present-or-not.html
http://automationplace.blogspot.in/2014/12/how-to-check-if-text-is-present-or-not.html
http://automationplace.blogspot.in/2014/12/selenium-webdriver-automation-logger.html
http://automationplace.blogspot.in/2014/12/selenium-webdriver-automation-logger.html
http://automationplace.blogspot.in/2014/12/read-xml-file-trigger-selenium-webdriver.html
http://automationplace.blogspot.in/2014/12/read-xml-file-trigger-selenium-webdriver.html
http://automationplace.blogspot.in/2014/12/selenium-webdriver-architecture.html
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=PopularPosts&widgetId=PopularPosts1&action=editWidget&sectionId=sidebar-right-1
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=Gadget&widgetId=Gadget1&action=editWidget&sectionId=sidebar-right-1
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=HTML&widgetId=HTML3&action=editWidget&sectionId=sidebar-right-1
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=ContactForm&widgetId=ContactForm1&action=editWidget&sectionId=sidebar-right-1
http://automationplace.blogspot.in/
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=PageList&widgetId=PageList1&action=editWidget&sectionId=footer-1
http://www.blogger.com/
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=BloggerButton&widgetId=BloggerButton1&action=editWidget&sectionId=footer-2-1
http://www.blogger.com/
http://www.blogger.com/rearrange?blogID=7649703008756780120&widgetType=Attribution&widgetId=Attribution1&action=editWidget&sectionId=footer-3

Hope you like this post.
HIT LIKE button on facebook.



2 comments: