Many sites have dynamic dropdown which will only show its list of option when the mouse been moved on top of it. Those elements might be hover only with no clicks enabled in them. This means we cannot traditional element click to load those contents in dropdown.
Selenium has a way to solve this problem which is using its ACTION class, which will help us simulate mouse moments in our test cases. Kindly go though the below code snippet.
Method - moveToElement
Code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Mouse_interaction {
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver","src\\Geko_driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.myntra.com/");
Actions a = new Actions(driver);
a.moveToElement(driver.findElement(By.xpath("(//a[@class='desktop-main'])[4]"))).build().perform();
}
}
Explanation
As explained above, we have to create a reference variable for our action class which is created like below,
Actions a = new Actions(driver);
We will be passing our Webdriver object into it. Then use moveToElement method and pass your locator, this is only a statement and this won't do anything so use build build() method to build the statement and then perform perform() method to perform the crated statements.
a.moveToElement(driver.findElement(By.xpath("(//a[@class='desktop-main'])[4]"))).build().perform();
statement
Method - dragAndDropBy
CODE
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Mouse_interaction {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:\\Software\\Selenium\\geckodriver-v0.24.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://jqueryui.com/droppable/");
WebElement source = driver.findElement(By.xpath("//div[@id='draggable']"));
WebElement destination = driver.findElement(By.xpath("//div[@id='droppable']"));
Actions a = new Actions(driver);
a.dragAndDropBy(source,160,24).build().perform();
}
}
EXPLANATION
yyy
Other Methods
Apart from moveToElement, we have listed few other use full methods of Action class, these are self explanatory.
Source : Link