2
  1. Hover over the element

  2. As soon as we hover there will be

    • A text box
    • A Submit button to click

Firstly, I have to hover with mouse and then enter the values in the Textbox and then click on Submit button.

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Jayant
  • 31
  • 5

2 Answers2

1

Please refer to the below code to perform the required operation.

// Initializing the action class
Actions action = new Actions(driver);

// Moving to the element
action.moveToElement(<WebElement>).build().perform();

// Entering the text in the text box
action.moveToElement(<WebElement of Textbox>).sendKeys("Text").build().perform();

// Clicking on the submit button
action.moveToElement(<WebElement of submit button>).click().build().perform();

or you can combine all the above actions into single action.

Actions action = new Actions(driver);
action.moveToElement(<Element which displayes text box>).moveToElement(<Element of textbox>).sendKeys("Text").moveToElement(<Element of submit button>).click().build().perform();

Hope this helps.

k.s. Karthik
  • 731
  • 6
  • 16
  • @Karthik, I have tried the above code and it is not working for me. – Jayant Jul 25 '16 at 10:38
  • @Automater, Yes I mean Hidden and it is only visible when I hover the mouse on it. – Jayant Jul 25 '16 at 10:41
  • @Jayant, Can you please paste me the error message observed – k.s. Karthik Jul 25 '16 at 11:08
  • @Karthik , Actually there is no error appearing below is how I have implemented the above code. - : Actions action = new Actions(driver); WebElement element = driver.findElement(By.id("panelCaller")); WebElement element1 = driver.findElement(By.id("txtappid")); WebElement element2 = driver.findElement(By.id("btnSearch")); action.moveToElement(element).build().perform(); action.moveToElement(element1).sendKeys("1149733").build().perform(); action.moveToElement(element2).click().build().perform(); – Jayant Jul 25 '16 at 11:27
  • Are you able to see the actions being performed on the UI? – k.s. Karthik Jul 25 '16 at 12:50
  • @Karthik, there are no actions performed on the UI – Jayant Jul 25 '16 at 12:54
  • Can you please put some wait before performing the first mouse hover and if possible can you please provide the site which you are trying, so that I will try from my end and will let you know – k.s. Karthik Jul 25 '16 at 12:55
  • @Karthik, I really appreciate you help but the thing which I am working on belongs to the client, I will try putting the wait before the mouse hover – Jayant Jul 25 '16 at 13:27
  • Have you tried adding a wait before the each actions you perform? – k.s. Karthik Jul 26 '16 at 11:04
0

(Amusing you are using java) According to your approach, you should try using Mouse to perform mouse over on the element then find element using WebDriverWait to wait until element visible as below :-

import org.openqa.selenium.interactions.HasInputDevices
import org.openqa.selenium.interactions.Mouse
import org.openqa.selenium.internal.Locatable;

WebDriverWait wait = new WebDriverWait(driver, 10);

//Find element first where you want to hover
WebElement hoverElement = wait.until(ExpectedConditions.visibilityOfElementLocated(byObject));
Mouse mouse = ((HasInputDevices)driver).getMouse();
mouse.mouseMove(((Locatable)hoverElement).getCoordinates()); //it will perform mouse over on desire element     

//Now after mouse over you can find the desire text box
WebElement textBox = wait.until(ExpectedConditions.visibilityOfElementLocated(byObject));
users.sendKeys("your value"); //It will set the value on text box

//Now you can find the desire submit button
WebElement submit = wait.until(ExpectedConditions.elementToBeClickable(byObject));
submit.click(); //It will click on submit button

Edited :- If unfortunately mouse over does not work using Mouse, you can use JavascriptExecutor to perform mouse over as below :-

JavascriptExecutor js = (JavascriptExecutor)driver;
executor.executeScript("function triggerMouseEvent (node, eventType) {"
                    + "var clickEvent = document.createEvent ('MouseEvents');"
                    + "clickEvent.initEvent (eventType, true, true);"
                    + "node.dispatchEvent (clickEvent);"
                    + "}triggerMouseEvent (arguments[0], 'mouseover');", hoverElement);

Hope it helps..:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • could you please let me know how below will work - builder.mouse.mouseMove(((Locatable)hoverElement).coordinates); //it will perform mouse over on desire element, As I am getting error in my code(The field Actions.mouse is not visible ) and (Locatable can not be resolved as a type) – Jayant Jul 25 '16 at 12:03
  • @Jayant please make sure you are correct imported as :- `import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.internal.Locatable;` – Saurabh Gaur Jul 25 '16 at 12:09
  • I have included all the latest Jar files of Selenium version 2.53.1 but I am still getting error for builder.mouse.mouseMove(((Locatable)hoverElement).coordinates) – Jayant Jul 25 '16 at 12:47
  • @Jayant and import?? – Saurabh Gaur Jul 25 '16 at 12:48
  • Yeahh done importing the above two org.openqa.selenium.interactions.Actions; import org.openqa.selenium.internal.Locatable; – Jayant Jul 25 '16 at 12:50
  • weird!! replace then it to `builder.moveToElement(hoverElement).build().perform();` and let me know.. – Saurabh Gaur Jul 25 '16 at 13:01
  • Yeah it worked for the mousehover but now I am getting error when it comes to the line where we are finding the text box where it gives an error like below - Timed out after 10 seconds waiting for visibility of element located by By.id: txtappid – Jayant Jul 25 '16 at 13:11
  • @Jayant let me know first after mouse over it will visible or not on the screen?? and try with the updated answer as well – Saurabh Gaur Jul 25 '16 at 13:14
  • I was running it on debug mode I dint get the error where the code for mouse over is written but error was there when it was searching for the text box which is only visible when the mouse over is done. Also I was not able to see the the mouse over happening on screen. Let me know if you want me to answer anything else – Jayant Jul 25 '16 at 13:18
  • @Jayant Yeah I know your mouse over not happening that's why you are not getting element..I have provided you to perform mouse over using `Mouse` interface..I have updated the answer try using `Mouse mouse = ((HasInputDevices)driver).getMouse(); mouse.mouseMove(((Locatable)hoverElement).getCoordinates());` to perform mouse over.. See updated answer..:) – Saurabh Gaur Jul 25 '16 at 13:24
  • now the error is appearing for the below line Mouse mouse = ((HasInputDevices)driver).getMouse(); HasInputDevices can not be resolved as a type – Jayant Jul 25 '16 at 13:31
  • @Jayant Strange with the problem.. I have provided you a alternate solution using `JavascriptExecutor` to perform mouse over.. try with updated answer and let me know..:) – Saurabh Gaur Jul 25 '16 at 13:56