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.



No comments:

Post a Comment