Selenium Locators

1.Write a Selenium script that opens the Firefox browser, maximizes the browser window, navigates to "http://google.com", prints the URL of the current page, reloads the page, and closes the browser.

import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver;

public class FirefoxExample { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver"); WebDriver driver = new FirefoxDriver();

try { driver.manage().window().maximize(); driver.get("http://google.com"); System.out.println("Current URL: " + driver.getCurrentUrl()); driver.navigate().refresh(); Thread.sleep(5000);

} catch (Exception e) { System.err.println("An error occurred: " + e.getMessage());

} finally { driver.quit(); } } }

2.Write a Selenium script that opens the Chrome browser, navigates to "https://www.demoblaze.com/", maximizes the browser window, verifies if the title of the page is "STORE", and prints "Page landed on correct website" if the title matches, else prints "Page not landed on correct website".

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

public class ChromeExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver();

try { driver.manage().window().maximize(); driver.get("https://www.demoblaze.com/");

// Verify if the title of the page is "STORE" String expectedTitle = "STORE"; String actualTitle = driver.getTitle();

if (actualTitle.equals(expectedTitle)) { System.out.println("Page landed on correct website"); } else { System.out.println("Page not landed on correct website"); }

} catch (Exception e) { System.err.println("An error occurred: " + e.getMessage());

} finally { driver.quit(); } } }

3.Write a Selenium script that opens the chrome browser, navigates to "https://www.wikipedia.org/", maximizes the browser window, searches for the query "Artificial Intelligence", clicks on the "History" section in the search results, and prints the title of the section.

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 WikipediaSearchExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver();

try { driver.manage().window().maximize(); driver.get("https://www.wikipedia.org/");

WebElement searchBox = driver.findElement(By.name("search")); searchBox.sendKeys("Artificial Intelligence"); searchBox.sendKeys(Keys.RETURN);

WebElement historyLink = driver.findElement(By.xpath("//a[text()='History']")); historyLink.click();

String sectionTitle = driver.findElement(By.id("firstHeading")).getText(); System.out.println("Title of the 'History' section: " + sectionTitle);

} catch (Exception e) { System.err.println("An error occurred: " + e.getMessage());

} finally { driver.quit(); } } }