Skip to main content

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

Learning How to Map One-to-Many Relationships in JPA Spring Boot with PostgreSQL

  Introduction In this blog post, we explore how to effectively map one-to-many relationships using Spring Boot and PostgreSQL. This relationship type is common in database design, where one entity (e.g., a post) can have multiple related entities (e.g., comments). We'll dive into the implementation details with code snippets and provide insights into best practices. Understanding One-to-Many Relationships A one-to-many relationship signifies that one entity instance can be associated with multiple instances of another entity. In our case: Post Entity : Represents a blog post with fields such as id , title , content , and a collection of comments . Comment Entity : Represents comments on posts, including fields like id , content , and a reference to the post it belongs to. Mapping with Spring Boot and PostgreSQL Let's examine how we define and manage this relationship in our Spring Boot application: Post Entity  @Entity @Getter @Setter @Builder @AllArgsConstructor @NoArgsCons

Tree Based Common problems and patterns

  Find the height of the tree. public class BinaryTreeHeight { public static int heightOfBinaryTree (TreeNode root) { if (root == null ) { return - 1 ; // Height of an empty tree is -1 } int leftHeight = heightOfBinaryTree(root.left); int rightHeight = heightOfBinaryTree(root.right); // Height of the tree is the maximum of left and right subtree heights plus 1 for the root return Math.max(leftHeight, rightHeight) + 1 ; } Find the Level of the Node. private static int findLevel (TreeNode root, TreeNode node, int level) { if (root == null ) { return - 1 ; // Node not found, return -1 } if (root == node) { return level; // Node found, return current level } // Check left subtree int leftLevel = findLevel(root.left, node, level + 1 ); if (leftLevel != - 1 ) { return leftLevel; // Node found in t

Stay Updated As Software Engineer

Are you a software engineer looking to stay updated and grow in your field? We've got you covered with over 50 valuable resources to keep you on the cutting edge of technology. From newsletters to books, we've curated a diverse list just for you.   Newsletters:   Pragmatic Engineer: Link   TLDR: Link   Level-up software engineering: Link   Coding challenges: Link   Engineers Codex: Link   Techlead Mentor: Link   Saiyan Growth letter: Link   Wes Kao: Link   Addy Osmani: Link   And many more (see link below)   Books:   Engineering:   A Philosophy of Software Design Link   Clean Code Link   Communication & Soft Skills:   Smart Brevity Link   Connect: Building Exceptional Relationships Link   Crucial Conversations Link   Engineers Survival Guide Link   Leadership:   The Manager's Path Link   Staff Engineer: Leadership Beyond the Management Track Link   The Coaching Habit: Say Less, Ask More Link   While we can't list all 50+ resources here, this is a fantastic sta