package Edureka;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Seleniumscript {
public static void main(String[] args) throws MalformedURLException,
IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\HP\\Downloads\\
selenium\\web driver\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// 2.Maximize the browser window
driver.manage().window().maximize();
// 1.Intialize a browser and open a particular website
driver.get("https://www.ebay.com/");
System.out.println("Title of the eBay page: " + driver.getTitle());
// 2.Automating actions on web form elements
WebElement searchField = driver.findElement(By.id("gh-ac"));
searchField.sendKeys("Laptop"); // Example input to the search field
WebElement searchButton = driver.findElement(By.id("gh-btn"));
searchButton.click();
// 4.Scrolling up and down
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,250)", "");
Thread.sleep(1000); // Adding a short delay to visualize the scroll
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,-250)", "");
// 5.Open a different menu item
WebElement motorsLink = driver.findElement(By.linkText("Motors"));
motorsLink.click();
// 6.Open a new tab and navigate to a new URL
((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); // Switch to new tab
driver.get("https://www.google.com/"); // Example URL for the new tab
// 7.Validating page titles and URLs
String ebayTitle = driver.getTitle();
String ebayURL = driver.getCurrentUrl();
System.out.println("Title of the eBay page: " + ebayTitle);
System.out.println("URL of the eBay page: " + ebayURL);
// You can repeat the above steps for another website and compare the
titles and URLs
// 8.Find broken links if any
List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
String href = link.getAttribute("href");
HttpURLConnection connection = (HttpURLConnection) new
URL(href).openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode >= 400) {
System.out.println(href + " is a broken link.");
} else {
System.out.println(href + " is a valid link.");
}
}
driver.quit(); // Quit the WebDriver session
}
}