I don't want to use action class as its failing for some browser. therefore I am using JS but I ended up with " arguments[0].hover is not a function" error for using arguments[0].hover();
WebElement ele = browser.driver.findElement(By.xpath(".//a[contains(text(),'Basic example')]"))
Actions builder = new Actions(browser.driver)
((JavascriptExecutor) browser.driver).executeScript("arguments[0].hover();", ele);
WebDriverWait wait = new WebDriverWait(browser.driver, 60);
WebElement selenium =wait.until(ExpectedConditions.visibilityOf(ele));
((JavascriptExecutor) browser.driver).executeScript("arguments[0].scrollIntoView();", ele);
waitFor(WAIT_TIME){$(By.xpath(".//a[contains(text(),'Basic example')]")).css("text-decoration-line") == "underline"}
assert $(By.xpath(".//a[contains(text(),'Basic example')]")).css("text-decoration-line") == "underline": "No underline is displaying after hover mouse"
Url tested on: https://developer.mozilla.org/en-US/docs/Web/CSS/:hover
2 Answers 2
https://www.w3schools.com/jsref/met_html_click.asp
click() is an action but hover() is a event , so calling hover over the element doesn't do anything . It is used define what should happen when user hovers over the element.
Using action class is the best way , but still if you want to use javascript use:
arguments[0].focus()
Note: in the example the link is inside iframe you should switch to that first
-
Using arguments[0].focus() it will only focus on the path given but basically, I want to move the mouse on the element given as I need to validate the colour interaction post mouse on so arguments[0].focus() didn't help much.meado– meado2021年02月14日 22:11:13 +00:00Commented Feb 14, 2021 at 22:11
-
why you don't want to use actions ?PDHide– PDHide2021年02月14日 22:11:46 +00:00Commented Feb 14, 2021 at 22:11
-
action class is passing with chrome, firefox browsers but not with Safari & IE, its throwing MoveTargetOutOfBoundsException error .meado– meado2021年02月14日 22:21:41 +00:00Commented Feb 14, 2021 at 22:21
-
So it's passing for all safari browser capability except Catalina safari 13.1 throwing an error MoveTargetOutOfBoundsExceptionmeado– meado2021年02月15日 00:48:33 +00:00Commented Feb 15, 2021 at 0:48
-
please add your code how are you moving to that elementPDHide– PDHide2021年02月15日 01:03:54 +00:00Commented Feb 15, 2021 at 1:03
In every testing company, automation testing services teams may face this type issue related to focusing a webElement. This can be done by doing mousehover actions by selenium or by java script.
Below is one way of getting the focus on webElement:
C#: ((IJavaScriptExecutor)webDriver).ExecuteScript("arguments[0].scrollIntoView(true);", webElement);
Java: ((JavaScriptExecutor)webDriver).executeScript("arguments[0].scrollIntoView(true);", webElement);
Here webElement is the element on which we are bringing the focus and can be passed as a variable like above.
Explore related questions
See similar questions with these tags.