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
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.Keys;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class Demo1 {
- public static void main(String[] args) throws InterruptedException {
//first paramter is driver name and second is driver's path
- System.setProperty("webdriver.chrome.driver",
- "C:\\New_Teslra\\DemoTipschromedriver\\chromedriver.exe");
- WebDriver driver = new ChromeDriver();
- driver.get("http://google.co.in");
- driver.findElement(By.name("q")).sendKeys("customizing surefire reports");
- driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
- Thread.sleep(5000);
- List<WebElement> list = driver.findElements(By.className("rc"));
- System.out.println("List"+list);
- for(WebElement element:list)
- {
- System.out.println(element.findElement(By.xpath(".//h3")).getText());
- //*[@id="rso"]/div/div/div[1]/div/div/h3
- //*[@id="rso"]/div/div/div[1]/div/div/h3
- //#rso > div > div > div:nth-child(1) > div > div > h3
- //System.out.println(element.findElement(By.tagName("h3")).getText());
- }
- //link.click();
- Thread.sleep(10000);
- driver.quit();
- }
- }
Comments
Post a Comment