Showing posts with label Actions class in Selenium. Show all posts
Showing posts with label Actions class in Selenium. Show all posts

Mouse hover Auto Suggestion in Selenium

Mouse Hover Actions in Selenium Webdriver. In order to perform a 'mouse hover' action, we need to chain all of the actions that we want to achieve in one go. So move to the element that which has sub elements and click on the child item. It should the same way what we do normally to click on a sub menu item.

Please refer below Program to understand how to perform mouse hover:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class MousehoverDemo {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://s1.demo.opensourcecms.com/wordpress/wp-login.php");
  Thread.sleep(1000);

  Actions act = new Actions(driver);

  driver.findElement(By.id("user_login")).sendKeys("opensourcecms");
  driver.findElement(By.id("user_pass")).sendKeys("opensourcecms");
  driver.findElement(By.id("wp-submit")).click();
  Thread.sleep(1000);

  WebElement logoutOption = driver.findElement(By.xpath("//a[text()='Howdy, ']"));

  act.moveToElement(logoutOption).perform();

  driver.findElement(By.xpath("//a[@class='ab-item'][text()='Log Out']")).click();

  Thread.sleep(2000);
  driver.close();

 }

}

Please refer below Program to understand how to handle Auto Suggestions in Selenium:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Autosuggestion {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://www.google.com/");
  Thread.sleep(2000);

  driver.findElement(By.name("q")).sendKeys("Selenium");
  Thread.sleep(3000);

  List<WebElement> list = driver.findElements(By.xpath("//ul/li[@class='sbct']"));

  for(int i = 0 ;i< list.size();i++)
  {
   System.out.println(list.get(i).getText());
   
   String searchText=list.get(i).getText();
   
   if(searchText.equals("selenium testing"))
   {
    list.get(i).click();
    break;
   }
  }

  driver.close();
 }

}

Please refer below youtube video to understand How to handle Mouse hover and Auto Suggestion.



Keyboard Operations using Selenium

Keyboard Events Using Selenium Actions Class API: We use below methods:
sendKeys(keysToSend) : sends a series of keystrokes onto the element.
keyDown(theKey) : Sends a key press without release it. Subsequent actions may assume it as pressed. (example: Keys. ALT, Keys. SHIFT, or Keys. CONTROL)
keyUp(theKey): Performs a key release.

Please refer below Program to understand how to perform keyboard operations:

public class KeyboardMouseActions {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;

  System.setProperty("webdriver.chrome.driver","C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

  driver.manage().window().maximize();

  driver.get("https://www.google.com/");

  Thread.sleep(1000);

  WebElement googleSearch=driver.findElement(By.name("q"));

 
  Actions act=new Actions(driver);

 
  Action action=act.keyDown(googleSearch, Keys.SHIFT)

  .sendKeys("selenium")

  .keyUp(googleSearch, Keys.SHIFT)

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("a")

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("x")

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("v")

  .build();

   action.perform();

                 Thread.sleep(3000);

  driver.close();

 }

}

Please refer below youtube video to understand How to handle Keyboard Operations: