Testing With Selenium WebDriver (72 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (62 Blogs)
SEE MORE

What is setProperty in Selenium and how can it be used?

Last updated on Jul 26,2023 27.9K Views

5 / 16 Blog from Selenium IDE Fundamentals

The primary basis for website testing is to instantiate the browser object and set the system properties of the browser drivers. That is achieved through the setProperty() method. In this article, I will tell you how does setProperty in Selenium works.

Below topics are covered in this article:

Let’s get started!

What is setProperty in Selenium?

setProperty, as the name says has two attributes which are – “System.setProperty(“propertyName”, “value”)”. It implies that it sets the system property ‘propertyName' to have the value 'value'.

While testing with Selenium, you will make use of the setProperty method because the browser doesn’t have a built-in server to run the automation code. In this case, you will need a Chrome/IE/Gecko driver server for communicating your Selenium code to the browser.

In simple words, to set the path of the driver for the respective browser you will need the system.setProperty.

Now let’s take a small example to understand how it works.

Alternatively, you can check out the Selenium course in Mumbai by Edureka and get certified!

Demo: Illustrating setProperty in Selenium

Take a look at the below code to know its working.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo {
public static void main(String[] args) {
// Here I am setting the system properties of chrome driver and specifying the path to it.
System.setProperty("webdriver.chrome.driver", "C:Selenium-java-edurekachromedriver_win32chromedriver.exe");
// Creating a object to instantiate the browser driver
WebDriver driver = new ChromeDriver();
//Navigating through a particular website
driver.get("https://www.ebay.com/");
//Locating elements using XPath locator for search box
driver.findElement(By.xpath("//input[@id='gh-ac']")).sendKeys("Guitar");
WebElement searchIcon = driver.findElement(By.xpath("//input[@id='gh-btn']"));//xpath for search button
searchIcon.click();
}
}

When you execute the above code, it will launch ebay website in Google Chrome using Chrome Driver where the initialization of the driver is handled by system.setproperty method. This has to be the first line of code that needs to be executed in your selenium script before any test method to initialize the driver. That’s how it works. If you wish to use Firefox Browser and Gecko Driver you can use them accordingly. Learn the working of Gecko Driver with the help of Gecko Driver in Selenium article.

I hope this gives you a clear understanding of how setProperty in Selenium works. Thus, it brings us to the end of this article. 

If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, live-online Selenium Certification, which comes with 24*7 support to guide you throughout your learning period.

Got a question for us? Please mention it in the comments section of setProperty in Selenium blog and we will get back to you.

Upcoming Batches For Selenium Certification Training Course
Course NameDateDetails
Selenium Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Selenium Certification Training Course

Class Starts on 20th May,2024

20th May

MON-FRI (Weekday Batch)
View Details
Selenium Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is setProperty in Selenium and how can it be used?

edureka.co