mouse hover is not working in safari ,selenium web driver - java

I used the Actions to mouse hover in FF and chrome , it is working fine. But the same code is not working in safari. I am using mac OS and selenium webdriver and java.
I tried below code.
new Actions(driver).moveToElement(element).build().perform();
JS:
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(mouseOverScript, element);
It is a show stopper issue. any help is appreciated.

Performing mouse hovers requires using WebDriver's Advanced User Interactions API. In the Java language bindings, this is accomplished by the Actions class. Unfortunately, at present, the SafariDriver does not implement the Advanced User Interactions API, so you cannot accomplish this directly. Until it is implemented, you could probably simulate it using JavaScript to fire the events fired my a mouse hover.

Related

How to click on Cancel on a windows explorer that displays when a upload button is clicked on a web page using Selenium WebDriver with Java

I am very new to Selenium WebDriver with Java. There is a Upload button on a job portal. When I click on that button windows explorer is displayed to choose the file. there are open and cancel buttons on this window. i want to select the cancel button. since it is a windows explorer i cannot inspect the cancel button. how do we write the code for cancelling the button. Thanks in advance.
driver.get("https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(
By.xpath("//[#id='container']/div/div/div[2]/div/div/div[2]/div/div[1]/div/div[1]/button")).click();
I do not have Java programming skills. I hope the below python code is easy to translate. In my opinion, combining windows action with selenium calls is often flaky.
There is a hidden element called 'upload resume button', you can change the attribute value to see it on the UI, use the send keys method on the element to upload the resume.
from selenium.webdriver import Remote, DesiredCapabilities
driver = Remote(desired_capabilities=DesiredCapabilities.CHROME)
driver.get('https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US')
driver.execute_script(
"document.getElementById('upload-resume-button').setAttribute('class', '')"
)
upload_your_resume = driver.find_element_by_id('upload-resume-button')
upload_your_resume.send_keys(r'C:\test\resume.docx')
The above code worked in my local.
Short answer is you cant, but if you want to upload a local file you can use send_keys on the input field, sending the file path.
Here is a python exemple:
driver.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")
you can use Robot class to simulate native keyboard and mouse actions to interact with windows based pop-ups.
The shortcut to close any opened window is: “Alt + Space +C” – Closes the focused window.
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_ALT);
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_SPACE);
rb.keyRelease(KeyEvent.VK_ALT);

Zoom In and Zoom out using selenium

I have a requirement, where I need to perform Zoom In and Zoom out using selenium webdriver. So this is the short cut key that i need to perform. Control+Shift+Add Key.
I tried the below code in chrome browser. But unable to perform action.
Could anyone have the right solution?
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.chord(Keys.ADD)).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).perform();
Chrome Version - 62
Selenium Version - 2.53
OS - Windows 7 and 10
If you want to do the zoom in chrome (not with the css) you can use the API. For example:
driver.get('chrome://settings/')
driver.execute_script('chrome.settingsPrivate.setDefaultZoom(1.5);')
driver.get("https://www.google.co.uk/")
EDIT
In Java:
System.setProperty("webdriver.chrome.driver", /pathTo/chromeDriver);
ChromeDriver driver = new ChromeDriver();
driver.get("chrome://settings/");
driver.executeScript("chrome.settingsPrivate.setDefaultZoom(1.5);");
driver.get("https://www.google.co.uk/");
If you are using headless chrome, you can add --force-device-scale-factor=1.5 argument to change the zoom level
You can use javascript to do this action (using css zoom):
For example (in python):
driver.execute_script("document.body.style.zoom = '200%'")
It works at least with Chrome.

Selenium Webdriver Java: Unable to perform click operation by Actions in Chrome

I am using Selenium Webdriver(Java) for my Automation. For one of my use-case, I need to click based on co-ordinates. I am using following code to perform this operation:
Actions act = new Actions(driver);
act.moveByOffset(236, 92).click().perform();
Above code is working perfectly in Firefox(Gecko driver). But with Chrome driver, it is not working. Any idea?
Is there any other way to perform this.
I think you can try what Santosh has suggested
act.moveByOffset(236, 92).click().build().perform();
However, this should not make much of a difference as the perform() already contains the build action but this might be a workaround for your problem.
If you can locate the webelement, you can use JavaScript to perform the click this way:
JavaScriptExecutor js = (driver)JavaScriptExecutor;
js.executeScript("arguments[0].click();", element);

jQuery / Javascript with seleniumw webdriver java

Is there any examples of Javascript or jQuery been used with Selenium Webdriver Java? I'm having issues with drag and drop using selenium. I'm hoping to use the following code with Javascript/jQuery to perform a drag and drop. I cannot use point and click in my test scripts to perform drag and drop so I'm hoping to use jQuery or java script to perform the drag and drop. I'm having trouble integrating the two. I only want suggestion for drag and drop using javascript or an example of javascript (any) and selenium webdriver code in the same script
public void test(){
driver.findElement(By.id("addService")).click();
driver.findElement(By.id("name")).sendKeys(name);
driver.findElement(By.id("identifier")).sendKeys(id);
driver.findElement(By.id("flowStatus")).clear();
driver.findElement(By.id("flowStatus")).sendKeys(flow);
}
public void dragAndDropElement(WebElement dragFrom, WebElement dragTo) throws Exception {
Actions actions = new Actions(driver);
actions.clickAndHold(dragFrom).release(dragTo).build().perform();;
}
public void test() throws Exception {
WebElement dragFrom = driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/form/fieldset/table[1]/tbody/tr/td[1]/div/div[1]"));
WebElement dragTo = driver.findElement(By.id("drop"));
dragAndDropElement(dragFrom,dragTo);
}
Have tried using Actions class? Here are docs. There are multiple ways you could drag and drop elements using it,
Actions actions = new Actions(driver);
actions.dragAndDrop(source,target).build().perform();
or
actions.clickAndHold(source).release(target).build().perform();
There are other ways as well, check docs and see which one is applicable for you. In general using javascript should be avoided when using WebDriver. WebDriver uses browsers native api's and simulate user interactions which are very close to a real user. Think of this, your user is not going to execute a javascript to do a drag and drop. I would suggest use it only when all other WebDriver doors are closed for you.

WebDriver, How do I prevent InternetExplorerDriver from taking control of the mouse pointer

Whenever I run a WebDriver test in IE (using InternetExplorerDriver), where elements of a web page need to be clicked, the InternetExplorerDriver takes over my mouse.
This does not happen when I run my tests in either Chrome or Firefox. Is there a way to prevent IE tests from taking control of the mouse pointer?
Example code that causes the mouse pointer to be 'stolen':
public void clickButtonNamed(String buttonName) {
driver.findElement(By.xpath("//div[contains(text(),'" + buttonName + "')]")).click();
}
public void dragAndDropElement(WebElement source, WebElement target) throws InterruptedException {
action.dragAndDrop(source,target).build().perform();
}
//IE Driver initialization
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS,true);
WebDriver driver = new InternetExplorerDriver(caps);
Is there a reason you set this property InternetExplorerDriver.REQUIRE_WINDOW_FOCUS to true? This is why IEDriver asks IE to take control your pointer.
To fix it, simply remove it or set it to false. This was introduced in 2.28.0.3, here's the quote from CHANGELOG:
Introduced the "requireWindowFocus" capability into the IE driver. When used in conjunction with the "nativeEvents" capability, the
driver will attempt to bring the current IE window to the
foreground before executing a mouse or keyboard event.
Also, when
the requireWindowFocus capability is set to true, advanced user
interactions will now use the Windows SendInput() API to execute
the interactions. To enable this behavior, set the value of the
requiresWindowFocus capability to "true" when creating an instance of
the IE driver. The default for this new capability is "false". This
functionality is currently considered extremely experimental; use
at your own risk.
Using this setting will avoid IE to take over the mouse control while performing actions -
InternetExplorerOptions ieOp = new InternetExplorerOptions();
ieOp.disableNativeEvents();

Categories