Handle browser dialog window without focus - selenium-webdriver

I have a Selenium WebDriver based script to automate file uploading. It uploads list of files one by one. I use AutoIT script to handle dialog window, file chooser window. Parameter $CmdLine[1] contains the path of actual file.
ControlFocus("Open a file","","Edit1")
ControlSetText("Open a file","","Edit1", $CmdLine[1])
ControlClick("Open a file","","Button1")
I execute it from Java code as following:
Runtime.getRuntime().exec(autoITExecutable);
It opens dialog window, so it can't work without focus on browser window.
File upload field works like this demo:
https://encodable.com/uploaddemo/

I ran simple script for the link you gave and it works great
import os
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://encodable.com/uploaddemo/")
driver.find_element_by_name("uploadname1").send_keys(os.getcwd() + "/test.csv")
driver.find_element_by_name("email_address").send_keys("none#getnada.com")
driver.find_element_by_name("first_name").send_keys("Tarun")
driver.find_element_by_id("uploadbutton").click()

Try your code in similar format as shown below and try:
WinWaitActive("File Upload") // enter the title of the pop up
Send("Path of the file to enter") // enter the path of the file to upload
Send("{ENTER}") / press enter

Related

File upload functionality of Selenium web driver for non-input button without AutoIt or Skuliii

I need to upload a document via Selenium WebDriver using Chromedriver. I have tried all the Action class and JavaScript stuff, but those do not work. I am assuming they do not work because those are relying on the button to be an input field, however, the upload button I'm dealing with is not. It's HTML looks like this:
Steps to reproduce:
Go to: https://www.fedex.com/apps/printonline/#!
Click on View Products under Marketing Material
Click on Get Started under Brochure
Click on Use your File to upload the file
Use Your File
I am able to click the use your file button, but I am not sure how I can upload the file.
driver.get("https://www.fedex.com/apps/printonline/#!");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
//Thread.sleep(6000);
if (driver.findElement(By.xpath("//area[#alt='close']")) != null) {
driver.findElement(By.xpath("//area[#alt='close']")).click();
}
driver.findElement(By.xpath("//a[#title='Marketing Materials']/child::button")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//a[#title='Get Started - Brochures']")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.xpath("//*[#class='btn fxg-btn-orange mycomputer-upload-link']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", element);
Ok so first of all get rid of those Thread.sleep(), use fluent wait with polling time, preferably as a function to locate the elements:
private WebElement waitFor(By locator) {
int timeout = 10;
FluentWait<WebDriver> wait = new FluentWait<>(driver)
.pollingEvery(Duration.ofMillis(200))
.withTimeout(Duration.ofSeconds(timeout))
.ignoring(NoSuchElementException.class);
return wait.until((driver) -> driver.findElement(locator));
}
Then you can click the buttons and upload the file like this:
waitFor(By.cssSelector("button.view-products")).click();
waitFor(By.cssSelector("a.get-started")).click();
waitFor(By.cssSelector("a.get-started")).click();
waitFor(By.cssSelector("input.file-upload")).sendKeys("path_to_my_file");
Notice I am using the input element to upload the file - I am not clicking the a link, as you do not need to do that. Just send the path directly to the input element.

Unable to Download csv file using werbdriver + java

I need to downlond csv file from my application. In my application there is table which contains rows and on right click of that row it display download option , on click on of that download it displays windows popup with open and save button.
To download csv from my application i have written below code but which is not working :(
WebDriver driver;
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "c:\\test");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
profile.setPreference("browser.download.useDownloadDir", false);
profile.setPreference("browser.download.manager.showWhenStarting", false);
driver = new FirefoxDriver(profile);
driver.get("Application link");
// Steps to click on Download file
Please Help.![enter image description here][1]
Try adding
profile.setPreference("browser.download.downloadDir", "c:\\test");
profile.setPreference("browser.download.defaultFolder", "c:\\test");
In addition - csv can come in many content types, not just text/csv..
Try using Firefox addon like HTTP headers to see the exact type the response.

Adobe Flex OpenWithDefaultApplication Movie

I am working on an Adobe Air application that I would like to be able to launch a movie using the default application. The code I am trying is:
var file:File = new File;
//currentMovie.ConfiguredPath = 'D:\\Movies\\TestMovie.avi';
file.nativePath = currentMovie.ConfiguredPath;
file.openWithDefaultApplication();
I have also tried this:
//currentMovie.ConfiguredPath = file://D:/Movies/TestMovie.avi'
navigateToURL(newURLRequest(currentMovie.ConfiguredPath));
The first option does nothing. No application opens, no errors, nothing. The second option worked but it launches a separate window, downloads the file to the local user's download directory then opens the file (as long as you click "Open"). This is not exactly the behavior that I was hoping for.

How to automate Uploading A file using webdriver.

I am trying to automate a file uploading using webdriver, my HTML is
it is of type file.
using firebug i got the id and it is same for textbox and button.
by using this command getWebDriverObj().findElement(By.id("fileupload")).sendKeys("code.txt"); am unable to fetch the result.
does any one faced this type of situation, if so can you please help me.
Thanks
Raghuram.
Autois Windows specific only.
Here is a more robust solution:
For this you will have find the "id" of the actual input box (where the path of the file is provided) and then use the following command:
driver.findElement(By.id("upload")).sendKeys("/path/to/the/file");
driver.findElement(By.id("upload_button")).click();
If you are using WebDriverBackedSelenium you can use:
selenium.type("locator", "/path/to/the/file");
selenium.click("upload_button");
If previous method is not working
You can try next chain.
1. Call File Select Dialog by click button (use webdriver method click() or javascript "document.getElementById('id').click()"
2. And send control to Autoit (or something another) and AutoIt will work with File Select Dialog (type addres to file, click button)
For example:
var Autoit = new AutoItX3();
const string widowTitle = "File Upload";
Autoit.WinWait(widowTitle, "File &name:", 10);
Autoit.ControlSetText(widowTitle, "", "[CLASS:Edit; INSTANCE:1]", pathToFile);
Autoit.ControlClick(widowTitle, "", "[CLASS:Button; INSTANCE:1]");
Autoit.WinWaitClose(widowTitle, "File &name:", 10);
Setup java and AutoIt http://code.google.com/p/autoitx4java/

Open a folder and highlight a particular file with WPF

Is there a way to launch an Explorer window and highlight a file in that folder with WPF ? I've already tried the following :
Process ExplorerWindowProcess = new Process();
ExplorerWindowProcess.StartInfo.FileName = "explorer.exe";
ExplorerWindowProcess.StartInfo.Arguments = ConfigFile.File.FullName;
ExplorerWindowProcess.Start();
... but that opens the file (in my case an XML file) with the default application in Windows Explorer, which I very much don't want. I know that the Aptana tools available for Eclipse allow you the ability to select a file in the Eclipse project browser and show the file in Explorer exactly as I want, but I need a way to implement this in my WPF app.
Explorer Command Line Arguments
http://support.microsoft.com/kb/152457
Explorer [/n] [/e] [(,)/root,<object>] [/select,<object>]
/n Opens a new single-pane window for the default
selection. This is usually the root of the drive Windows
is installed on. If the window is already open, a
duplicate opens.
/e Opens Windows Explorer in its default view.
/root,<object> Opens a window view of the specified object.
/select,<object> Opens a window view with the specified folder, file or
application selected.
You will also want to put quotes around the filename like so:
startInfo.FileName = "explorer.exe";
startInfo.Arguments = "/select,\"" + ConfigFile.File.FullName + "\"";

Resources