Friday, February 9, 2018

how to perform mouse hover in selenium web driver

There will be a situation where you need to hover on a menu item to see the sub items listed under it. Selenium might not recognize the link text to be selected within the short fraction of time the sub menu item visible. In this case we can use Action class to move the focus to the sub menu item to help selenium in recognizing the web element.

Below code snippet can be used to achieve this

WebElement element = driver.findElement(By.linkText("Menu"));

Actions action = new Actions(driver);

action.moveToElement(element).perform();

WebElement subElement = driver.findElement(By.linkText("SubMenu"));

action.moveToElement(subElement).build().perform();

how to verify if WebElement exists in selenium webdriver

You can use any of the below 3 functions to verify if a particular web element exists in your web application using selenium web driver.

Sample code for the same looks like below:

driver.findElement("your locator code here").isDisplayed();

This function returns Boolean value "TRUE" if the web element as per the locator exists and displayed in your application. else it will return False.

In the same way, you can use isSelected(), isEnabled() functions to check if a web element is selected or enabled in your web application. 

Wednesday, February 7, 2018

How to highlight the WebElement using selenium WebDriver

You want to highlight an element/object in webpage using the selenium webdriver. add the below code in your function to highlight the web element

WebElement we = driver.findElement(By.xpath("//div[@name='firstempty']/b"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].style.border='3px solid red'", we);

The above code can be used for any web element provided available in the web page and able to identify using the given locator

Monday, January 29, 2018

How to Scroll down the webpage using Selenium WebDriver

You can include the below code snippet to scroll the webpage down in Selenium WebDriver

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;

public class Day1 {

WebDriver driver;
JavascriptExecutor jse;
public void invokeBrowser()
{
try {
System.setProperty("webdriver.chrome.driver", "C:\\\\Drivers\\\\chromedriver_win32\\\\chromedriver.exe");
driver= new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("https://www.google.com");
searchCourse();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void searchCourse()
{
try {
driver.findElement(By.id("homeSearchBar")).sendKeys("Java");
driver.findElement(By.id("homeSearchBarIcon")).click();
jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0,1050)");
Thread.sleep(2000);
driver.findElement(By.xpath("//label[contains(text(),'Weekend')]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//label[contains(text(),'Weekday')]")).click();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Day1 myobj = new Day1();
myobj.invokeBrowser();

}

}

Looking for Selenium tips and tricks? coming soon...

Are you looking for help in Selenium tips and troubleshooting techniques.


Follow this blog for some interesting information and updates