Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver and browsers select file dialog

Tags:

I'm using selenium webdriver, C#.

Is it possible to make work webdriver with Firefox select file dialog? Or must I use something like AutoIt?

like image 754
Oleg Strokatyy Avatar asked Jan 13 '12 13:01

Oleg Strokatyy


People also ask

How do you use CTRL C and CTRL V in Selenium automation?

Copy & Paste Text: When we need to copy some text from one text box to another, we select the text by pressing "CTRL+A" they copy the text using "CTRL+C" and paste the text in the new text box by simply clicking in the text box and pressing keys "CTRL+V".


2 Answers

If you are trying to select a file for upload Selenium 2 supports HTML file inputs. For example:

HTML

<input type="file" id="uploadhere" /> 

Selenium Code

IWebElement element = driver.FindElement(By.Id("uploadhere")); element.SendKeys("C:\\Some_Folder\\MyFile.txt"); 

Basically you "type" (with SendKeys) the full file path to the file input element. Selenium handles the file selection dialog for you.

However if you want to manipulate an arbitrary file selection dialog, then like Anders said, you have to go outside of Selenium.

like image 81
prestomanifesto Avatar answered Sep 18 '22 08:09

prestomanifesto


No, WebDriver cannot interact with dialogs - this is because dialogs are the domain of the operating system and not the webpage.

I know people that have had luck with autoit as well as the Automation API provided by .Net.

Another option would be to skip the file dialog entirely and issue a POST or a GET, but this requires more advanced knowledge of the website as well as understanding how construct a POST/GET.

You could try Webinator, it is similar to Selenium in the sense that it is powered by WebDriver. It provides file dialog capabilities and I've had great success with it.

like image 38
Anders Avatar answered Sep 18 '22 08:09

Anders