Clicking the Web Element and Scraping the Details

Hi, guys in this Tutorials I am going to show you how to click a web element and scrap the details.

Follow the below steps 

1. Create a Java project and download the jars selenium-server-standalone-2.41.0.jar

2.Add the downloaded Jar in your project.

3.Download the Chrome Drive for use in the program below.

4 Create the program for scraping

  1. import java.util.List;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.Keys;
  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.WebElement;
  6. import org.openqa.selenium.chrome.ChromeDriver;
  7. public class Demo1 {
  8. public static void main(String[] args) throws InterruptedException {
//first paramter is driver name and second is driver's path
  1. System.setProperty("webdriver.chrome.driver",
  2. "C:\\New_Teslra\\DemoTipschromedriver\\chromedriver.exe");
  3. WebDriver driver = new ChromeDriver();
  4. driver.get("http://google.co.in");
  5. driver.findElement(By.name("q")).sendKeys("customizing surefire reports");
  6. driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
  7. Thread.sleep(5000);
  8. List<WebElement> list = driver.findElements(By.className("rc"));
  9. System.out.println("List"+list);
  10. for(WebElement element:list)
  11. {
  12. System.out.println(element.findElement(By.xpath(".//h3")).getText());
  13. //*[@id="rso"]/div/div/div[1]/div/div/h3
  14. //*[@id="rso"]/div/div/div[1]/div/div/h3
  15. //#rso > div > div > div:nth-child(1) > div > div > h3
  16. //System.out.println(element.findElement(By.tagName("h3")).getText());
  17. }
  18. //link.click();
  19. Thread.sleep(10000);
  20. driver.quit();
  21. }
  22. }


Comments

Popular posts from this blog

12 Must-Know LeetCode+ Links for Coding Excellence

Stay Updated As Software Engineer

Getting Master in Hibernate and JPA