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

Software Testing

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

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

Last updated on Sep 10,2024 28.3K 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.

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.

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.

Find out our Selenium Training in Top Cities/Countries

IndiaOther Cities/Countries
BangaloreUS
HyderabadUK
PuneCanada
ChennaiAustralia
MumbaiSingapore
KolkataEdinburgh

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 Course
Course NameDateDetails
Selenium Course

Class Starts on 16th December,2024

16th December

MON-FRI (Weekday Batch)
View Details
Selenium Course

Class Starts on 21st December,2024

21st December

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