0% found this document useful (0 votes)
20 views4 pages

Java Selenium 25 Programs

The document provides a list of 25 frequently asked questions related to Java with Selenium, each accompanied by code snippets. It covers various actions such as launching a browser, handling elements like buttons and dropdowns, managing alerts, and performing mouse actions. Additionally, it includes methods for waiting, taking screenshots, and closing browser windows.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Java Selenium 25 Programs

The document provides a list of 25 frequently asked questions related to Java with Selenium, each accompanied by code snippets. It covers various actions such as launching a browser, handling elements like buttons and dropdowns, managing alerts, and performing mouse actions. Additionally, it includes methods for waiting, taking screenshots, and closing browser windows.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java with Selenium - 25 Frequently Asked Questions with Code

### Java with Selenium - 25 Questions with Code

1. Launch Chrome Browser

WebDriver driver = new ChromeDriver();

driver.get("https://www.google.com");

2. Maximize Browser

driver.manage().window().maximize();

3. Get Page Title

System.out.println(driver.getTitle());

4. Navigate to URL

driver.navigate().to("https://www.example.com");

5. Click a Button

driver.findElement(By.id("submit")).click();

6. Enter Text in Input Field

driver.findElement(By.name("username")).sendKeys("admin");

7. Get Text from Element

String text = driver.findElement(By.id("welcome")).getText();

System.out.println(text);
8. Get Attribute Value

String value = driver.findElement(By.id("logo")).getAttribute("alt");

9. Handle Dropdown

Select dropdown = new Select(driver.findElement(By.id("country")));

dropdown.selectByVisibleText("India");

10. Handle Checkbox

WebElement checkbox = driver.findElement(By.id("terms"));

if (!checkbox.isSelected()) checkbox.click();

11. Handle Radio Button

driver.findElement(By.id("male")).click();

12. Handle Alert

Alert alert = driver.switchTo().alert();

alert.accept();

13. Switch to Frame

driver.switchTo().frame("frameName");

14. Switch Back from Frame

driver.switchTo().defaultContent();

15. Mouse Hover using Actions

Actions actions = new Actions(driver);

WebElement element = driver.findElement(By.id("menu"));


actions.moveToElement(element).perform();

16. Right Click

Actions actions = new Actions(driver);

actions.contextClick(driver.findElement(By.id("btn"))).perform();

17. Double Click

Actions actions = new Actions(driver);

actions.doubleClick(driver.findElement(By.id("dblClick"))).perform();

18. Drag and Drop

Actions actions = new Actions(driver);

WebElement src = driver.findElement(By.id("drag"));

WebElement tgt = driver.findElement(By.id("drop"));

actions.dragAndDrop(src, tgt).perform();

19. Scroll Down

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("window.scrollBy(0,500)");

20. Capture Screenshot

File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(src, new File("screenshot.png"));

21. Wait - Implicit Wait

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
22. Wait - Explicit Wait

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit")));

23. Close Browser

driver.close(); // closes current tab

24. Quit All Browser Windows

driver.quit();

25. Get All Links on Page

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

System.out.println("Total links: " + links.size());

You might also like