How to click save in IE download bar by selenium? - selenium

Here the snapshot the ie download explorer
I have tried using robot but not working.
try
{
Robot robot = new Robot();
robot.setAutoDelay(250);
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_F4);
}
catch (AWTException e)
{
e.printStackTrace();
}
I have tried using System.out.println to check if the code navigates to the robot function. But the screen seems to be stuck after the line that prompting that download.
Is the current window not active ? Because if i try to click any element without saving the file it is not able to click.
Code to download is an icon
driver.findElement(By.xpath("//input[#name='ctl00$TrackerPlaceHolder$btnexcel']")).click();

You have to use CTRL+S keystroke instead of ALT+S.

Related

Alternate ways to upload files with no input tag. Java selenium

DOM doesn't include input tag for upload button. Upload button does not work manually unless I have Web Capture Service software downloaded. If I disable the web capture service and try to click the upload button, nothing happens no dialog box pop up nothing. I have tried to see if an input tag is created after the fact of uploading a file, but nope.
Solution
Whenever we don't have input tag , we use Robot Class. I am adding code below & you just need to call uploadFile Method in your code.
i.e uploadFile("C://test.pdf") ;
public static void setClipboardData(String string)
{
/StringSelection is a class that can be used for copy and paste operations.
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
public static void uploadFile(String fileLocation)
{
try{
//Setting clipboard with file location
setClipboardData(fileLocation);
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (Exception exp)
{
exp.printStackTrace();
}
}
Hope it helps you.Keep Learning.Keep Growing.Best wishes.

Click the element does not invoke next event

The problem is when the element is clicked, it does not open the pop-up it is suppose to open.The floating menu disappears and hence fails on the next step
I used
System.out.println("RateType_selected");
to validate the click
(The element which this command clicks is on a floating menu and disappears if the page scroll)
I am not scrolling the page just clicking the element to open the pop-up but the menu disappears when clicked
// Command Element to open the floating menu
driver.findElement(By.cssSelector(".create-for-no-item .create-new.add-rate")).click();
// Command to click on the floating menu element to open popup
driver.findElement(By.xpath("//li[#class='new-item popup-opener active-new-list-item']//span[#class='new-text'][contains(text(),'Recurring Rate')]")).click();
driver.findElement(By.cssSelector(".action-dropdown-button:nth-child(3) > .create-new")).click();
// Tried Javascript as well
WebElement addRatetype = driver.findElement(By.cssSelector(".action-dropdown-button:nth-child(3) > .create-new"));
js.executeScript("arguments[0].click();", addRatetype);
((JavascriptExecutor) driver).executeScript("arguments[0].click();",driver.findElement(By.cssSelector(".action-dropdown-button:nth-child(3) > .create-new")));
// Also, tried the try catch
try {
driver.findElement(By.cssSelector(".action-dropdown-button:nth-child(3) > .create-new")).click();
System.out.println("Clicked on Recurring rate");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I used a new locator to find the element and now licks everytime
driver.findElement(By.xpath("//div[#class='create-for-no-item product-create rate sdfcddm need-to-hide-dropdown-panel']//li[2]")).click();

Close download bar

I am using Java and Selenium to write a test. Somewhere in my test, I download a file but then need to click on a button under the download bar which appears at the bottom of the chrome browser page. I search a lot but the only solution was here which is not my case as I don't have a scroll.
I also use:
action.sendKeys(Keys.CONTROL+ "j").build().perform();
action.keyUp(Keys.CONTROL).build().perform();
Thread.sleep(500);
ArrayList<String> tabs2 = new ArrayList<String> (driverChrome.getWindowHandles());
driverChrome.switchTo().window(tabs2.get(1));
Thread.sleep(500);
driverChrome.close();
driverChrome.switchTo().window(tabs2.get(0));
Thread.sleep(500);
but it doesn't open the download page.
Anyway that I can close the download bar?
This method did not work for me either, but I developed a workaround. I do any download test in a new window, then close the download window, the original window does not have the download bar. It must be a new window, if you do a new tab it will transfer over, to get this I use JavaScript. Switch to the new window, run download test and then switch to the original window when done.
string javascript = $"$(window.open('', '_blank', 'location=yes'))";
((IJavaScriptExecutor)Driver).ExecuteScript(javascript); //create new window
Driver.SwitchTo().Window(Driver.WindowHandles.Last())); //switch to new window
//do download test here
Driver.Close(); //close created window
Driver.SwitchTo().Window(Driver.WindowHandles.First()); //back to original window with no download bar

Clarification on Java robot commands

I am automating file upload in Selenium using Java Robot. Below is my code.
StringSelection stringSelection = new StringSelection(imageFileName);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot;
try {
robot = new Robot();
//imitate mouse events like ENTER, CTRL+C, CTRL+V
robot.delay(250);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException e) {
logger.debug(e);
logger.debug("Error while dealing with non-browser window");
}
The problem occurs if in the window "Recent" option is selected.
If in the window it points to "Computer" then I get the location search bar where I can paste the file path and click on open.
Please let me know how to handle if in the window it is pointing to anything other than "Computer". If it is pointing to something else I want to click on Computer and execute.

How to handle windows download popup for IE11 using Selenium Webdriver without using AutoIT

While testing on IE11, When I click on the link to download a doc, a new blank window opens up with save and Cancel option popup. I want to hit cancel, Switch back to my current window and continue validation.
Can anyone help me how to handle this without the use of AutoIT.
If you are using selenium-webdriver with Java then you can use Sikuli-api.
Just a small introduction: Sikuli is a tool which helps to achive automation task for any software(web/standalone). Base of Sikuli is a Screenshot of the control you would like to interact with.
In your case, it is just matter of 2 buttons. Hence I would not suggest you to use seperate autoIT generated *.exe file.
Just take screenshot of OK & Cancel button.
Showing you the sample code here:
import org.sikuli.script.*;
public class Test {
Screen m_screen;
SikuliScript m_sikscr;
#Test
public void Test1() throws FindFailed
{
m_screen=new Screen();
m_screen.wait((double) 10.0);
//Click on Cancel button
m_screen.click(new Pattern("./img/CancelButton.png"));
}
}
This much should do your task.
Please Note, if at all you decide to use this method, make sure that you handle all Sikuli related dependencies in java project.
Try manually at first whether by pressing escape key it dismissing the popup.
if so follow the below code,
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
r.keyPress(KeyEvent.VK_ESCAPE);