Handling Dynamic fields in Salesforce using Selenium

| By Webner

Selenium is an open-source (free) automated testing framework for validating web-based applications across various browsers and platforms. Selenium Test Scripts can be written in a variety of programming languages, including Java, C#, Python, etc.

Authorizing OTP Verification using selenium:

Before heading towards automating the salesforce processes, we must get a way around automating the OTP Verification, and we can use two methods for OTP Verification:

  1. Providing the Selenium Webdriver Access to your Email using an API.
  2. Manually entering the OTP, the further code will only run if we manually enter the correct OTP.

//Code to Manually Enter OTP:
public class LoginTest {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver();
//Initialize WebDriverWait first which will wait maximum 60 seconds
WebDriverWait wait = new WebDriverWait(driver, 60);
//Create suitable locator to locate textbox element with xpath locator
By byObject = By.xpath("//*[@id=\"emc\"]");
//This condition will wait until textbox has value greater or equal 6 character
wait.until(new ExpectedCondition() {
public Boolean apply(WebDriver d) {
return (d.findElement(byObject).getAttribute ("value").length() >= 6);

Automating the Text Fields with dynamic IDs:

ven though the ID of a dynamic field changes every time, you can see if there is a specific order in which the ID is changing.

Example: //input[@id=’input-126′],

Here, the number (126) in the id “input-126” changes on every refresh so we can not locate the element by ID. But we can customize the XPath to look for the element which starts with an id “input-”.

this will locate all the elements starting with ID “input-”. So if there are multiple fields starting with this ID, we have to use list elements to get the list of all fields and then select the one we need using for loop on field Text.

If this method is not working, we can use full XPath or relative XPath to access the element.

Handling the lookup Fields:

Lookup fields in salesforce, if we enter any text in the lookup field, it will narrow the search and suggest results. In order to get the lookup results from dynamically changing lists, we can not just hard code the ID/path for the result we want as it can change. Also, if we just simply click on the lookup field, the results will not be the same every time as it only shows the recently viewed items. So, in order to automate a lookup field, we first need to send the text into the lookup field so it can show us the list of results. Then, we can look for a specific text/result we want from the list and select it.


//Handling Lookup Field
driver.findElement(By.xpath("path to the field")).sendKeys("text you want to search for");
List<WebElement> list = driver.findElements(By.xpath("path to the list items(“li Tag”) under ul tag"));
System.out.print("total number of results:" + list.size());
for(int i=0; i<list.size(); i++) {
if(list.get(i).getText().contains("specific text you want to search for")) {

Leave a Reply

Your email address will not be published. Required fields are marked *