0% found this document useful (0 votes)
5 views12 pages

Chrome Driver SetProperty Usage

The document provides several Java code examples using Selenium WebDriver to automate web browser actions, including opening websites, searching for tutorials, and implementing cross-browser functionality. It demonstrates the use of Chrome and Edge drivers, along with various locators like ID and XPath for interacting with web elements. Each example includes a problem statement, corresponding code, and instructions for execution.

Uploaded by

khadeeja.n
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)
5 views12 pages

Chrome Driver SetProperty Usage

The document provides several Java code examples using Selenium WebDriver to automate web browser actions, including opening websites, searching for tutorials, and implementing cross-browser functionality. It demonstrates the use of Chrome and Edge drivers, along with various locators like ID and XPath for interacting with web elements. Each example includes a problem statement, corresponding code, and instructions for execution.

Uploaded by

khadeeja.n
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

Problem Statement:

Opening a website on Chrome driver


Solution:
Code :
package BATCH1;
import [Link];
import [Link];
//import [Link];
public class DemoBrowser {
​ public static void main(String[] args) {
​ ​ // TODO Auto-generated method stub
​ ​ [Link]("[Link]",
"D:\\Selenium\\chromedriver-win64\\[Link]");
​ ​ WebDriver wd = new ChromeDriver();
​ ​ [Link]("[Link]
​ ​ [Link]().window().maximize();
​ ​
​ ​ //[Link]("[Link]",
"D:\\Selenium\\[Link]");
​ ​ //WebDriver wd = new EdgeDriver();
​ ​ //[Link]("[Link]
​ ​ }
}
Output :
Problem Statement:
Opening a tutorial point on a web driver and search for python tutorial using id.
Code :
package BATCH1;
import [Link];
import [Link];
import [Link];
//import [Link];
public class DemoLocator {
​ public static void main(String[] args) {
​ ​ // TODO Auto-generated method stub
​ ​ [Link]("[Link]",
"D:\\Selenium\\chromedriver-win64\\[Link]");
​ ​ WebDriver wd = new ChromeDriver();
​ ​ [Link]().window().maximize();
​ ​
[Link]("[Link]
=Cj0KCQjwhMq-BhCFARIsAGvo0KcC3EHJXoNAjQJCIQX1xU87Hv
Gwz0P72GgJH9LWcwnIdpNbDfqu1uQaAk-dEALw_wcB");
​ ​
[Link]([Link]("search-strings")).sendKeys("python");
​ ​ [Link]([Link]("search-strings")).sendKeys("\n");
​ ​
​ ​
​ ​ //[Link]("[Link]",
"D:\\Selenium\\[Link]");
​ ​ //WebDriver wd = new EdgeDriver();
​ ​ //[Link]("[Link]
​ ​ }
}
Output :
Problem Statement:
Opening a W3school on a web driver and search for python tutorial using xpath.
Code :

package BATCH1;
import [Link];
import [Link];
import [Link];
public class DemoSearch {
​ public static void main(String[] args) throws
InterruptedException {
​ ​ // TODO Auto-generated method stub
​ ​ [Link]("[Link]",
"D:\\Selenium\\chromedriver-win64\\[Link]");
​ ​ WebDriver wd = new ChromeDriver();
​ ​ [Link]("[Link]
​ ​ [Link]().window().maximize();
​ ​
[Link]([Link]("/html/body/div[5]/div[1]/div/div/div/div[1]
/div/form/input")).sendKeys("Python tutorial");
​ ​ [Link](2000);
​ ​
[Link]([Link]("/html/body/div[5]/div[1]/div/div/div/div[1]
/div/form/button")).click();
​ ​
​ }
}
Output :
Problem Statement:
Cross Browsing using switch case.
Code :
package BATCH1;
import [Link];
import [Link];
import [Link];
import [Link];
public class DemoCrossBrowser {
​ public static void main(String[] args) {
​ ​ Scanner sc = new Scanner([Link]);
​ ​ // TODO Auto-generated method stub
​ ​ [Link]("1- Chrome");
​ ​ [Link]("2 - Edge");
​ ​ int i =[Link]();
​ ​ switch(i) {
​ ​ case 1 : ​ ​
​ ​ ​ [Link]("[Link]",
"D:\\Selenium\\chromedriver-win64\\[Link]");
​ ​ ​ WebDriver wd = new ChromeDriver();
​ ​ ​ [Link]("[Link]
​ ​ ​ [Link]().window().maximize();
​ ​ ​ break;
​ ​
​ ​ case 2 : ​ ​
​ ​ ​ [Link]("[Link]",
"D:\\Selenium\\[Link]");
​ ​ ​ WebDriver wd1 = new EdgeDriver();
​ ​ ​ [Link]("[Link]
​ ​ ​ [Link]().window().maximize();
​ ​ ​ break;
​ ​ }
​ ​
​ }
}

Output :
On Choosing option 1 :

On Choosing option 2 :
20/3/25
package batch1;
import [Link];
import [Link];
​ import [Link];
public class demolocator {​
​ ​ public static void main(String[] args) {
​ ​ ​ // TODO Auto-generated method stub
​ ​ ​ [Link]("[Link]",
"D:\\Selenium\\chromedriver-win64\\chromedriver-win64\\[Link]");
​ ​ ​ WebDriver wd = new ChromeDriver();
​ ​ ​ [Link]("[Link]
​ ​ ​ [Link]([Link]("search-strings")).sendKeys("hello");
​ ​ ​ [Link]([Link]("search-strings")).sendKeys("/n");
​ ​ ​ [Link]().window().maximize();
​ ​ }
​ }

package Batch1;
import [Link];
import [Link];
import [Link];
import [Link];
public class DemoCrossBrowser {
​ public static void main(String[] args) {
​ ​ // TODO Auto-generated method stub
​ ​ Scanner sc = new Scanner([Link]);
​ ​ [Link]("1: Chrome");
​ ​ [Link]("2: Edge");
​ ​
​ ​ int ch = [Link]();
​ ​ switch(ch) {
​ ​ case 1:
​ ​ ​
[Link]("[Link]","D:\\Selenium\\chromedriver-win64\\chromedriver-win64\\c
[Link]");
​ ​ ​ WebDriver wd = new ChromeDriver();
​ ​ ​ [Link]("[Link]
​ ​ ​ [Link]().window().maximize();
​ ​ ​ break;
​ ​ ​
​ ​ case 2:
​ ​ ​
[Link]("[Link]","D:\\Selenium\\edgedriver_win64\\edgedriver_win64\\msedge
[Link]");
​ ​ ​ WebDriver wd2 = new EdgeDriver();
​ ​ ​ [Link]("[Link]
​ ​ ​ [Link]().window().maximize();
​ ​ ​ break;
​ ​ default:
​ ​ ​ [Link]("Invalid Option!");
​ ​ }
​ }
}

package Batch1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class DemoSearch {
​ public static void main(String[] args) {
​ ​ // TODO Auto-generated method stub
​ ​
[Link]("[Link]","D:\\Selenium\\chromedriver-win64\\chromedriver-win64\\c
[Link]");
​ ​ WebDriver wd = new ChromeDriver();
​ ​ [Link]("[Link]
​ ​ [Link]().window().maximize();
​ ​
[Link]([Link]("/html/body/div[5]/div[1]/div/div/div/div[1]/div/form/input")).sendKeys("Python
tutorial");
​ ​ [Link]([Link]("//*[@id=\"learntocode_searchicon\"]")).click();
​ }
}

Common questions

Powered by AI

The code ensures compatibility with multiple browsers by setting up different WebDriver instances based on user input through a switch-case structure. Each case specifies which browser to launch by setting the system property to the path of the appropriate driver executable for each browser, be it Chrome or Edge, and then initiates the WebDriver for that browser. This dynamic browser selection allows the automation script to run on different browsers without modifying the core logic .

Setting incorrect paths for browser drivers in Selenium WebDriver configurations results in the WebDriver being unable to locate the required driver executable, leading to runtime errors and preventing the browser from launching. Consequently, this halt in script execution disrupts the test process and requires corrections to the configuration path before proceeding with any automated tasks .

To perform a search operation using an element locator strategy in Selenium WebDriver, you first locate the search input element on a webpage using an appropriate locator such as 'By.id', 'By.xpath', etc. After locating the element, you can send the desired text to it with 'sendKeys()' method and simulate a search operation with actions like pressing 'Enter' or clicking a search button element .

XPath is advantageous in Selenium because it provides the ability to locate dynamic and complex elements within a webpage's DOM, helpful when elements have no specific 'id' or 'name'. XPath can navigate up and down the DOM, access elements with variable attributes, and use logical conditions to precisely locate elements. This flexibility makes XPath powerful, albeit slightly slower, compared to simple locators like ID or Name .

Selenium WebDriver handles dynamic content by allowing scripts to wait for certain conditions to be met using WebDriverWait and ExpectedConditions classes. This enables scripts to wait for elements to become visible, clickable, or present in the DOM before performing actions, thus accommodating dynamically loaded elements or content that change due to AJAX requests. This approach helps in synchronizing tests with the application's state .

The 'System.setProperty' method is used in Selenium WebDriver code to set the system property for the specific browser driver executable file, such as 'chromedriver.exe' or 'msedgedriver.exe'. This step is crucial because it notifies the WebDriver about the location of the driver executable so that it can launch the browser instance for automation tasks .

Element interactions in Selenium WebDriver, such as clicking a button, are handled by first locating the element using an appropriate locator strategy (e.g., By.id or By.xpath). Once the element is identified, the 'click()' method is invoked on the element to simulate a user clicking it. This action can trigger form submissions, page navigations, or AJAX requests depending on the webpage's design .

Using magic number indices in XPath locators can lead to brittle Selenium scripts because changes in the page structure, like adding or removing elements, can alter the position of elements, thus invalidating the indices. This practice reduces the robustness and maintainability of test scripts, as updates to the DOM can result in the need to frequently revise these indices, making element identification unreliable .

Maximizing a browser window during automated tests in Selenium is significant because it ensures that all elements are visible and interactable without layout restrictions that can occur in smaller viewports. This reduces issues related to element overlap, hidden elements, or responsive design affecting test consistency. By maximizing the window, tests can more reliably interact with the page elements irrespective of the display characteristics .

Selenium WebDriver can be configured for cross-browser testing using a switch-case statement by first taking user input to decide which browser to use. Then, inside the switch-case, each case corresponds to a different browser, setting its respective driver properties, such as 'chromedriver' for Chrome and 'msedgedriver' for Edge. The code then initializes a WebDriver instance for the selected browser and performs the desired operations, making it possible to switch between different browsers based on user input .

You might also like