Selenium Webdriver v-3.0.1
I want to close the browser window using hot keys, I have tried following methods one by one but not working at all -
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL+"w"));
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL+"F4"));
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL,Keys.F4));
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL, Keys.F4);
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL+"F4");
How do I close browser other then driver.close() and driver.quit method
Make sure browser window should be active.
You can perform using actions
Actions actions = new Actions(driver);
actions.keyDown(Keys.ALT);
actions.sendKeys(Keys.F4);
actions.keyUp(Keys.ALT);
actions.perform();
Same you can achieve using Robot.
To get this code running you need to add,
import java.awt.Robot;
Robot robot = new Robot();
// press key Alt+F4
robot.keyPress(KeyEvent.VK_ALT);
robot.delay(100);
robot.keyPress(KeyEvent.VK_F4);
// relase key Alt+F4
robot.delay(100);
robot.keyRelease(KeyEvent.VK_F4);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_ALT);
If you do not use driver.quit() at the end of program, WebDriver session will not close properly and files would not be cleared off memory. This may result in memory leak errors.
Hope it is clear.
Assuming you have opened a single tab you can try below method:
driver.findElement(By.xpath("/html/body")).sendKeys(Keys.CONTROL+"w");
Actions actions = new Actions(driver);
String killBrowser= Keys.chord(Keys.ALT, Keys.F4);
actions.sendKeys(killBrowser);
actions.perform();
Try this.
Related
Could like to press character 'v' in keyboard through robot which works fine as expected in browser mode but not working in headless mode .
Trying to loop throught list of elements and screenshot it post keypress . I am using Robot class to press the character 'v' which is not working instead it prints v in output.
I am using firefox- geckodriver-v0.24.0-win64 driver and my browser version is v69.0.1 .
List<WebElement> eleq = driver.findElements(By.cssSelector(".class"));
JavascriptExecutor js = ((JavascriptExecutor) driver);
for(WebElement e: eleq){
js.executeScript("arguments[0].scrollIntoView(true);", e);
Actions builder = new Actions(driver);
Action seriesOfActions = builder
.moveToElement(e)
.build();
seriesOfActions.perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_V);
Thread.sleep(1000);
Date d =new Date();
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File(d.toString().replace(":", "_")+".png"));
Thread.sleep(3000);
robot.keyPress(KeyEvent.VK_V);
}
Could like to know if there are any alternatives to keypress in Java selenium apart from Robot since most of my search led to Robot not working in headless.
Of-course it will not as Robot do the event without seen any other dependency.
Even if you minimize the your automation browser screen it won't work as it is independent from everything
That's why using Robot is not recommended in automation.
You need to identify other way to complete your step
you can do something like below:
String selectAll = Keys.chord(Keys.CONTROL, "a");
driver.findElement(By.id("your locator")).sendKeys(selectAll);
I have achieved this using Actions in selenium.
I am using selenium webdriver with Java to automate the webpages
When I enter the url, I am getting the authentication required dialog box
I am able to enter the username and password by configuring profile
but I am not able to click on OK button
Note: Not able to get the ok button property so am not able to
use the below code
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
Is there any other way to press on ok button through webdriver?
You need to handle it as an alert box,
wait for popup to appear and click OK.
Below code waits up to a maximum of 10 seconds for the popup to be present and then accepts it by clicking OK. Although wait is optional.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Handling credential boxes is not possible directly using Selenium You can use the JAVA AWT robot class to press enter. This class is available in the java API itself.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
Alternatively, you can use AutoIt or an image based testing tool like SIKULI http://www.sikuli.org.
Please note that when you are using these solutions, the workstation screen cannot be locked while running the test cases.
Try this code snippet:
driver.findElement(By.xpath("//body")).sendKeys(Keys.RETURN);
It will definitely work.
I need to press control+mouse click keys using Selenium WebDriver(java). I need to select multiple element in my script.
Is there any way to do it?
I checked the Selenium libraries and found that selenium allows key press of special and functional keys only.
There is already written library Actions in WebDriver which you can use.
Short Description of what is happening:
First you are pressing the Control button and then you are clicking (in this case) 3 times on your defined WebElemen objects) then your are unpressing the Control and finish your Actions.
In this case you can achive the selection of 3 items (or opening a 3 new tabs) depending on what your WebElements are.
Actions actions = new Actions(driver);
actions.keyDown(Keys.LEFT_CONTROL)
.click(first_WebElement)
.click(second_WebElement)
.click(third_WebElement)
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();
Do it with the help of 'Actions' as below:
Actions action=new Actions(driver);
action.keyDown(Keys.CONTROL).build().perform();
driver.findElement(By.xpath(".//*[#id='selectable']/li[1]")).click();
driver.findElement(By.xpath(".//*[#id='selectable']/li[3]")).click();
action.keyUp(Keys.CONTROL).build().perform();
In the case of using Mac, te code would be next:
action.keyDown(Keys.COMMAND)
.click(WebElement)
.keyUp(Keys.COMMAND)
.build()
.perform();
You achieve same using jquery code
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "e = jQuery.Event('click');e.ctrlKey = true; $('secondRow_Css_locator').trigger(e);";
js.executeScript(script);
OR you can also use robot class but it can lock your screen for a moment
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
As of 2018 the is the first results pops up. Earlier it was working fine after FF 61 (direct jump form 47 to 61) It starts breaking. unfortunately none of the answer worked for me. Solved it by action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform(); just iterating every element one by one
it worked with me using this:
Actions action=new Actions(driver);
action.keyDown(Keys.CONTROL).build().perform();
driver.findElement(By.xpath(".//*[#id='selectable']/li[1]")).click();
driver.findElement(By.xpath(".//*[#id='selectable']/li[3]")).click();
action.keyUp(Keys.CONTROL).build().perform();
I'm using Selenium Webdriver in Java and I want to verify if is possible to download one document. When I click a link, its shown pup up download window and I need verify text from the title and close it. But I cannot click at the popup window and I don't know XPath etc. http://postimg.org/image/si2eagaqr/
driver.close(); this statement is useless for me
Can anyone advise me? I will be very grateful.
You can configure Firefox the directly download the files - File types and download actions
If you don't want to hardcode the setting for your browser, you can setup a specific FF profile only for your tests, where you can configure where you want the files to be downloaded.
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
WebDriver driver = new FirefoxDriver(firefoxProfile);
I suggest you to use the first approach is much simpler.
Hello you can use robot for this purpose by pressing Enter please call the function below to press enter and it will press download button on UI . If this not works please let me know key sequence to perform this action .
public void pressEnter() throws AWTException, InterruptedException {
Thread.sleep(5000);
Robot rb=new Robot();
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER); }
I need to Send ALT+S Key event using Selenium Web Driver for an ``EditBox. Cursor Position is already set to EditBox I am using following code
driver.switchTo().activeElement().sendKeys(Keys.chord(Keys.ALT+"S"))
but it's not giving me desired result. It's Typing character 'S' in the Edit Box.
I have tried another code but got the same result.
Actions action =new Actions(driver);
action.keyDown(Keys.ALT).sendKeys(String.valueOf('\u0053')).perform();
Thanks in Advance
I want to Add one more thing here. The code is working Properly in Firefox 12 but its not working properly in IE9
Cross-browser issues are rather hard to investigate as they are specific to particular driver and not WebDriver API.
Another variant that might work.
driver.findElement(By.xpath("your editbox's XPath")).sendKeys(Keys.chord(Keys.ALT, "s"));
As workaround I might recommend to take a look to AutoIT (Official site) or Robot (Java Doc)
Try this. It might work, I haven't tried though
driver.findElement(By.xpath("your editbox's XPath"))
.sendKeys(Keys.chord(Keys.ALT + Keys.S));
You can achieve this by using Robot class of java
try{
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}