limited time offer

Selenium Interaction with Web Elements Interview Questions

Showing 1 - 10 of 10 results

Can we enter text without using sendKeys() in Selenium WebDriver?

Yes. We can enter/send text without using sendKeys() method. We can do it using JavaScriptExecutor.

Using DOM method of, identification of an element, we can go to that particular document and then get the element by its ID (here username) and then send the text by value. Look at the sample code below:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById("username").value=myusername");

How to send ALT/SHIFT/CONTROL key in Selenium WebDriver?

When we generally use ALT/SHIFT/CONTROL keys, we hold onto those keys and click other buttons to achieve the special functionality. So it is not enough just to specify keys. ALT or keys. SHIFT or keys. CONTROL functions.

For the purpose of holding onto these keys while subsequent keys are pressed, we need to define two more methods:  and 


keyDown(modifier_key)

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose: Performs a modifier key press and does not release the modifier key. Subsequent interactions may assume it's kept pressed.

keyUp(modifier_key)

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose: Performs a key release.

Hence with a combination of these two methods, we can capture the special function of a particular key.

Example

public static void main(String[] args)
{
    String baseUrl = "";
    WebDriver driver = new FirefoxDriver();
    
    driver.get("baseUrl");
    WebElement txtUserName = driver.findElement(By.id(“Email”);

    Actions builder = new Actions(driver);
    
    Action seriesOfActions = builder
    .moveToElement(txtUerName)
    .click()
    .keyDown(txtUserName, Keys.SHIFT)
    .sendKeys(txtUserName, “hello”)
    .keyUp(txtUserName, Keys.SHIFT)
    .doubleClick(txtUserName);
    .contextClick();
    .build();
    seriesOfActions.perform();
}

How to handle keyboard and mouse actions using Selenium?

We can handle special keyboard and mouse events by using Advanced User Interactions API. The Advanced User Interactions API contains the Actions and the Action Classes that are needed for executing these events. Most commonly used keyboard and mouse events provided by the Actions class are mentioned below:

  • clickAndHold(): Clicks (without releasing) the current mouse location.
  • dragAndDrop(): Performs click-and-hold at the location of the source element, moves.
  • source, target(): Moves to the location of the target element, then releases the mouse.

What is the use of JavaScriptExecutor in Selenium?

JavaScriptExecutor is an interface which provides a mechanism to execute Javascript through the Selenium WebDriver. It provides "executescript" and "executeAsyncScript" methods, to run JavaScript in the context of the currently selected frame or window. An example of that is:

JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript(Script,Arguments);

How will you use Selenium to upload a file?

If the file is on the same machine or in a mapped network drive, it is really straightforward: We need to type the "path" of the file in the FileUpload control. For example:

//Locate file selection input field 
WebElement uploadElement = driver.findElement(By.id(""));

// enter the file path onto the file-selection input field
uploadElement.sendKeys("C:\\myfile.txt");

// click the "UploadFile" button
driver.findElement(By.name("send")).click();

Write a Selenium script to count number of search result with pagination.

Click Read more to View Code 

int productCount = 0;
List<WebElement> elements;
	    
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//nav[@class='_1ypTlJ']/a")));
	   
// number of Pages
elements = driver.findElements(By.xpath("//nav[@class='_1ypTlJ']/a"));
	
for (int i = 0; i < elements.size()-1; i++) 
{
    // Click on ith Page
    elements.get(i).sendKeys(Keys.ENTER);
  
   // List of products on page i
    List<WebElement> productElement = driver.findElements(By.xpath("//div[@class='_3wU53n']"));
    
    // Count products on pages
    productCount = productCount + productElement.size();     
}

Follow This Link for complete program

Subscribe to Selenium Interaction with Web Elements Interview Questions