How to mouseover on a webelement using Selenium WebDriver with Java - java

How to perform a mouse hover functionality using Selenium Webdriver?
Test Case is like say, open Yahoo site and there is link (Mail) beside Sign-In.
Upon mouse hover it will show a tooltip.
When i try the below code, it is not mouse hovering the exact location, rather it hovers somewhere else. Where i am going wrong?
And also let me know, how to capture the Tooltip?
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 Sample
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.yahoo.com");
driver.manage().window().maximize();
try
{
Thread.sleep(5000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement lMail=driver.findElement(By.xpath("//*[#title='Mail']"));
Actions builder=new Actions(driver);
builder.moveToElement(lMail).build().perform();
}
}

Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

Try this code:
//Assume driver initialized properly.
WebElement element = driver.findElement(By.id("Element id"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());

I have used similar code and it works for me.
I have also used the following at a few places:
browser.executeScript("jQuery('mycss-selector').mouseover();")
You will have to use css-selector, instead of xpath.

Related

How to perform multiple actions and click on the link with text as Member Login on the url http://www.spicejet.com/ through selenium-webdriver

I tried the below code but it is not mouse hovering and clicking on 'Member login'
WebElement lgn = driver.findElement(By.id("ctl00_HyperLinkLogin"));
WebElement ssm = driver.findElement(By.xpath("//a[contains(text(), 'SpiceCash/SpiceClub Members')]"));
WebElement cgm = driver.findElement(By.xpath("//a[contains(text(),'Member Login')]"));
Actions a1 = new Actions(driver);
a1.moveToElement(lgn).moveToElement(ssm).moveToElement(cgm).click().build().perform();
To invoke click() on the element with text as Member login, first you have to Mouse Hover over the element with text as LOGIN / SIGNUP, then Mouse Hover over the element with text as SpiceCash/SpiceClub Members then induce WebDriverWait for the element with text as Member Login to be clickable and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Spicejet_member_login {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.spicejet.com/");
new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.link#ctl00_HyperLinkLogin")))).build().perform();
new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[#class='hide-mobile']/a[contains(.,'SpiceCash/SpiceClub Members')]")))).build().perform();
new WebDriverWait(driver, 7).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#class='hide-mobile']//ul/li/a[#href='https://book.spicejet.com/Login.aspx' and contains(.,'Member Login')]"))).click();
}
}
Browser Snapshot:
You can try to add waits between your moveToElement() calls
WebDriverWait wait = new WebDriverWait(WebDriverRunner.getWebDriver(), 10);
wait.until(ExpectedConditions.visibilityOf(element))
where the "element" is your menu that should appear on hover.
Or you can use ready solution Selenide framework which is built on top of the Selenium and has built in hover method and waits which help to handle page dynamics
By this link you can find an example of hover() method usage.

Selenium WebDriver -- XPath

Trying to get the script to select the filter button on the top but can't seem to figure out how to input the XPath. I believe it has something to do with it be in a separate iframe.
package chromebrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavaClass {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Newfolder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://mlvtwrew73.consilio.com/Relativity/");
driver.manage().window().maximize();
//Thread.sleep(5000); this can be used as a wait command before moving on to the next function
WebElement objWE;
Thread.sleep(9000);
// objWE = driver.findElement(By.linkText("User Status"));
// objWE.click();
driver.switchTo().defaultContent();
driver.findElement(By.xpath("id(\"ctl00_ctl00_itemList_FilterSwitch\")")).click();
// objWE = driver.findElement(By.id("1"));
// driver.close(); will be used to close the site once all testing completes
}
}
Use ID locator -- it's more appropriate here (and faster than XPath):
WebDriverWait wait= new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ct‌l00_ctl00_itemList_F‌​ilterSwitch")));
driver.findElement(By.id("ctl00_ctl00_itemList_FilterSwitch")).click();
Looks like you're passing in an incorrect XPath. Try this one:
driver.findElement(By.xpath("//a[#id='ctl00_ctl00_itemList_FilterSwitch']")).click();
If there is then why you are using xpath.
Use id like this
driver.findElment(By.id("ctl00_ctl00_itemList_FilterSwitch"). click ();
I need more clarity regarding your question.
As I have understand, I think you need a requirement of XPath of the Filters image. If i am right, try this:
//div[#class='actionCellContainer']//a/img[#class='itemListActionImage']

How to set focus on element on moving the mouse over it in Selenium WebDriver using Java?

I am a beginner in Selenium WebDriver and I'm using this test on the StackOverflow homepage. These will be the steps of my test:
Firstly, go to the homepage of the StackOverflow.
Then, move the mouse on the Users button such that it will be focused. (We don't have to click on it just hover the mouse over it.)
Now, find the active element on the screen (i.e. the element which is
currently focused) and then click on it.
I want the Users button to be clicked because it is focussed currently but that's not happening. Instead the Questions button is clicked on.
Here is my code for the same test.
package insertCartridge;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "D:\\SELENIUM\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
String baseUrl = "https://stackoverflow.com/";
driver.get(baseUrl);
driver.manage().window().setSize(new Dimension(1920, 1080));
WebElement Users = driver.findElement(By.xpath("/html/body/header/div/div[1]/nav/ol/li[5]"));
Thread.sleep(3000);
Actions builder = new Actions(driver);
builder.moveToElement(Users).perform();
Thread.sleep(3000);
driver.switchTo().activeElement().click();
}
}
I am not able to understand why I am not getting the expected output. Please help.
I don't know why Actions class is not focusing the element. Its an issue or something as so many time i have trouble with Actions class in Firefox browser.
Still you have one alternative way to Focus on desired element and then perform click on focused element i.e. JavascriptExecutor
Here you is your code :
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('nav-users').focus();");
System.out.println(driver.switchTo().activeElement().getTagName());
driver.switchTo().activeElement().click();

From the Right Click menu it is not clicking on a specific option Selenium

In an project, I want to right click on an option and from there I want to choose "Open Link in New Window". I have written the following selenium-java code. In this code it is crawling on the "Open Link in New Window" but after that it is not clicking that option to open my required link in a new window. If you want you can directly copy and paste my code to visualize the execute flow. Please help me to understand where is the mistake I have done....My intention is to open a link in new window
package pack_001;
import java.util.Set;
import java.util.concurrent.TimeUnit;
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;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
public class mail {
public static WebDriver driver=new FirefoxDriver();
public static void main(String args[])
{
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.tutorialspoint.com/java/");
driver.manage().window().maximize();
WebElement env=driver.findElement(By.linkText("Java - Environment Setup"));
System.out.println("Env point out");
Actions oAction = new Actions(driver);
oAction.moveToElement(env);
oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).click().build().perform(); /* this should click on the option*/
}
}
HI instead of click use ENTER it will work
oAction.moveToElement(env);
oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
also why click will not work cause click clicks in middle of the given webElement and new Tab option is not a webElement
This will surely open the tab in new windwo
UPDATE
public class DropDownSelection {
public static WebDriver driver=new FirefoxDriver();
public static void main(String[] args) {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// i have used a sample url exactly as similar to your problem
driver.get("http://desirulez.biz/category/wrestlingnetwork/wwe/wwe-raw/");
driver.manage().window().maximize();
// way One
Actions oAction = new Actions(driver);
// now simply hover over the WWE - Menu in your case
WebElement Menu = driver.findElement(By.xpath("//*[#id='menu-item-8']/a"));
oAction.moveToElement(Menu).build().perform();
// hovering will open its sub-menu - Configure in your case
// now identify the sub-menu that you want to click/hover
WebElement Configure = driver.findElement(By.xpath("//*[#id='menu-item-8']/ul/li[7]/a"));
oAction.moveToElement(Configure).build().perform();
// now hovering over it will open its sub menu
// in your case Manage
// now identify the sub-menu that you want to click/hover
WebElement Manage = driver.findElement(By.xpath("//*[#id='menu-item-8']/ul/li[7]/ul/li/a"));
oAction.moveToElement(Manage).click().build().perform();
// way Two
// note if you want to chain above you can even do that like below
oAction.moveToElement(Menu).moveToElement(Configure).moveToElement(Manage).click().build().perform();
}
}
Hope this solves your query

How to mouse hover and click on element in webdriver

I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.
I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.
I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.
I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.
package nxusdata;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
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.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class MouseHoveringworpress {
public static WebDriver driver;
#BeforeTest
public void openinBrowser(){
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://wordpress.com/wp-login.php?redirect_to=https%3A%2F%2Fwordpress.com%2F");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void menus() throws InterruptedException{
driver.findElement(By.xpath("//*[#id='user_login']")).sendKeys("ritu7180");
driver.findElement(By.xpath("//*[#id='user_pass']")).sendKeys("jaiguruji123");
driver.findElement(By.xpath("//*[#id='wp-submit']")).click();
//*[#id='header']/a[2]/span
Thread.sleep(15000);
driver.findElement(By.xpath("//*[#id='header']/a[1]/span")).click();
//*[#id='secondary']/div/ul/li[4]/ul/li[5]/a/span[1]
Thread.sleep(4000);
driver.findElement(By.xpath("//*[#id='secondary']/div/ul/li[4]/ul/li[5]/a/span[1]")).click();
Thread.sleep(10000);
Set <String> windows =driver.getWindowHandles();
System.out.println(windows.size());
//Print the size of windows
Iterator<String> it = windows.iterator();
//iterate through your windows
while (it.hasNext()){
String parentwindow = it.next();
System.out.println("This is first window id "+parentwindow);
String childwindow = it.next();
System.out.println("this is second window id "+childwindow);
driver.switchTo().window(childwindow);
// driver.findElement(By.xpath("//*[#id='save-post']")).click();
Thread.sleep(4000);
WebElement menu = driver.findElement(By.xpath("//*[#id='menu-links']/a/div[3]"));
WebElement SUBMenu = driver.findElement(By.xpath("//*[#id='menu-links']/ul/li[2]/a"));
Actions action = new Actions(driver);
action.moveToElement(menu).perform();
Thread.sleep(2000);
action.click(SUBMenu).perform();
}
}
}
You can try something like this:
Actions actions = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//*[#id='menu-links']/a/div[3]"));
actions.moveToElement(menu);
WebElement subMenu = driver.findElement(By.xpath("//*[#id='menu-links']/ul/li[2]/a"));
actions.moveToElement(subMenu);
actions.click().build().perform();
Learn more here.
// Locating the Main Menu (Parent element)
WebElement mouseHoverOnLocations = driver.findElement(By.partialLinkText("Locations"));
// Instantiating Actions class
Actions actions = new Actions(driver);
// Hovering on main menu
actions.moveToElement(mouseHoverOnLocations);
// Locating the element from Sub Menu
WebElement selectSantaClara = driver.findElement(By.linkText("Santa Clara"));
// To mouseover on sub menu
actions.moveToElement(selectSantaClara);
// build()- used to compile all the actions into a single step
actions.click().build().perform();

Categories