0% found this document useful (0 votes)
52 views

Misc

This Java program uses Selenium to count and print the number of links on a web page. It launches ChromeDriver, navigates to a test URL, finds all link elements by tag name, prints the size of the list, and iterates through printing the text of each link. A second class finds the number of elements in an unordered list after searching Google, prints the text of each result, and clicks the desired result by text if found.

Uploaded by

akshay gundewar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Misc

This Java program uses Selenium to count and print the number of links on a web page. It launches ChromeDriver, navigates to a test URL, finds all link elements by tag name, prints the size of the list, and iterates through printing the text of each link. A second class finds the number of elements in an unordered list after searching Google, prints the text of each result, and clicks the desired result by text if found.

Uploaded by

akshay gundewar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

package misc;

import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class NoOfLinksOnWebPage {

public static void main(String[] args) throws InterruptedException {


System.setProperty("webdriver.chrome.driver", "D:\\Velocity\\Java
Class\\26th March B\\Selenium\\chromedriver.exe");

WebDriver driver= new ChromeDriver();


driver.manage().window().maximize();
driver.get("https://vctcpune.com/selenium/practice.html");

Thread.sleep(2000);

List<WebElement> links = driver.findElements(By.tagName("a"));

System.out.println(links.size());

Iterator<WebElement> it = links.iterator();
while(it.hasNext())
{
System.out.println(it.next().getText());
}

}
package misc;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class UnorderedList {

public static void main(String[] args) throws InterruptedException {


System.setProperty("webdriver.chrome.driver", "D:\\Velocity\\Java
Class\\26th March B\\Selenium\\chromedriver.exe");

WebDriver driver= new ChromeDriver();


driver.manage().window().maximize();
driver.get("https://www.google.co.in/");

Thread.sleep(2000);

driver.findElement(By.name("q")).sendKeys("honda");
Thread.sleep(200);

List<WebElement> searchResults =
driver.findElements(By.xpath("(//ul[@class='G43f7e'])[1]//li"));

System.out.println(searchResults.size());

for(WebElement r:searchResults)// for getting text only


{
System.out.println(r.getText());
}

for(WebElement result:searchResults)// for clicking on required result

{
String actualText = result.getText();
String expectedText = "honda amaze";

if(actualText.equals(expectedText))
{
result.click();
break;
}
}

driver.findElement(By.linkText("Images")).click();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy