4

I'm trying to use the Selenium module in python to generate a text list from one website, save it in a directory, and browse to that text list on another site to submit it.

I'm working on the script in two parts- 1. Get metadata and 2. Order data. I've successfully completed the script in part 1, except for the very last thing: Choosing to save the metadata file that was just generated. I left it alone to work on part 2, hoping I would stumble upon the answer, but I'm just reaching to same problem when the pop-up to choose file comes along.

In the documentation, I'm told that Selenium WebDriver has built-in support for handling popup dialog boxes and that after triggering a dialog box, if I call alert = driver.switch_to_alert() then I can "accept, dismiss, read its contents, or even type into a prompt."

However, it's not working. When I try alert.text('some text') or alert.send_keys(Keys.TAB), I keep getting the error NoAlertPresentException: Message: No alert is present and after adding the command to wait, I get the error TimeoutException: Message:

Are the popups I'm getting (screenshots attached) not recognized by Selenium? If so, how do I interact with them? It seems like using this to save and/or upload files is something that many people have to do, but I cannot find anything on Google. Specifically, I would like to choose 'Save File' then 'OK' for the first image and for the second I would like to browse to the file (i.e. enter the path into the file name field) and click 'Open.' I don't want to just change my Firefox settings to automatically save because this will eventually be run in a different environment, and that won't help solve my second problem.

Thanks!

EDIT: I'm testing my script on windows but it will eventually be implemented on a linux cloud server. I thought I was going to have to switch to PhantomJS webdriver (which was probably going to make my problem worse) to do headless browsing but I found a way to keep firefox. I guess all this means is that I can't use AutoIT to fix my problem.

save dialog

user20408
  • 679
  • 2
  • 7
  • 23
  • driver.switchto.alert(in Java) we use for JavaScript dialogs. They are alertbox, conformationbox and Popupbox. But the dialog you are seeing is not belongs to any of the above, so you cannot handle with Alert. For detailed explanation of alerts you can watch video https://www.youtube.com/watch?v=3T0YaroB2fk. You may need to try AutoIT etc.. for ur problem. – Uday Jan 06 '15 at 03:53
  • You can try out something equivalent to Robot class in java. – Vivek Singh Jan 06 '15 at 06:44

2 Answers2

5

The popups you see are not regular popups that can be interacted with using switch_to. These popups are system dialogs and cannot be automated using selenium.

Usually people avoid having these dialogs shown in the first place by tweaking browser preferences, e.g.:

For uploading, usually you can find the appropriate input element and send keys to it with a path to the file:

Let me know if your case cannot be solved by using the answers in the links I've attached.


As for your first, "download file automatically" problem, you just need to set a correct content-type:

profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/xml,text/xml")

Second problem fix (upload part):

driver.find_element_by_name("input_product_list").send_keys(textpath)
driver.find_element_by_name('include_sr').click()
driver.find_element_by_id('submit').click()
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Hi alec, thanks for your response. I've looked at every single selenium/upload files thread that I can find and still no solution. I went to all three of your links RE tweaking browser preferences and none of them worked for me-- the pop-up box to open/save file still appears. For uploading, I've tried finding the input element, clicking and sending keys in every way possible, but that didn't work either. I think the reason for that is, if you go to the second website I linked to, you'll see there is no text box for the path to the file. You can only open a file by clicking browse and... – user20408 Jan 07 '15 at 22:47
  • ...entering the pathname there/browsing to the file interactively. So since selenium can't interact with these OS dialog boxes, is there another module that can? Or another hack to work around this issue? Please note that I added an edit describing how this will be implemented in linux, so I can't use autoIT. Literally the most frustrating thing I've worked on yet, as I'm so close to being done if it were not for this one problem. Urgh. – user20408 Jan 07 '15 at 22:49
  • @user20408 we'll solve it, don't worry. Let's do it step by step though. For starters, let's work on the download part - can you show the code you have for clicking that download `metadata.xml` file button - so that I can test it? Thanks. – alecxe Jan 08 '15 at 01:14
  • Thanks alec, I really appreciate your willingness to help. Would it be better for me to just send the script your way? How should I go about doing that? I don't have enough rep to do much of anything here. Thank you. – user20408 Jan 08 '15 at 14:17
  • @user20408 just edit the question and include it there. If it is large, create a gist and post a link here. Thanks. – alecxe Jan 08 '15 at 14:19
  • here's a link to 'OrderMetadata.py:' https://gist.github.com/mrwooten/9bda94f3490e5c01ab1d. Please keep in mind that this is just a test script so it's pretty messy. Thanks! – user20408 Jan 08 '15 at 14:22
  • @user20408 updated with a solution, check it out. What about the upload part? Can you provide the details? – alecxe Jan 08 '15 at 14:45
  • Hi alec, I can't see what got changed. I clicked on the revision link on the side but in comparing it to my script, I don't see any changes. Forgive me if I'm just overlooking it or not looking in the right place. Will add the second script to the gist now. Thx – user20408 Jan 08 '15 at 15:30
  • alec, just kidding, I see you edited it in your answer. Taking a look now. – user20408 Jan 08 '15 at 15:33
  • OK so, first of all, yay! I tested it on the cloud and it worked. The only thing is, I now have two files in my directory: metadata.xml (0bytes) and metadata.xml.part (142 KB when it's supposed to be 1580KB). I'm guessing my script is ending/cutting the connection before it's done downloading? Would adding a wait help with this issue, and if so, is there a way to ensure the file is downloaded all the way before ending? Perhaps adding a statement that says 'wait until metadata.xml.part is gone? – user20408 Jan 08 '15 at 15:44
  • alec, while I'm reading about waits (see previous comment), I've also added SubmitOrder.py to the gist. This step requires an account on the website, which I have, but would prefer not to put out on the internet. Is there a way I can PM you with that info? Also, I've attached a sample text file to the gist that is in the correct format to be submitted in this step. Although what's in the actual file isn't relevant, (I just need to know how to actually add a file), I like testing as close to the real thing as possible. Thanks! – user20408 Jan 08 '15 at 15:58
  • Regarding your latest issue with `WebdriverWait`, try this: `wait.until(lambda: not os.path.isfile(os.path.join(path, 'metadata.xml.part')))`. – alecxe Jan 08 '15 at 20:00
0

Extremely simple implementation using AutoIT.

Below scripts and steps can hely you to click on 'Save>OK' on this window's pop-UP

Step 1: Download AutoIT package/tool here AutoIt You may select ZIP format (extract it)

Step 2: Open any text editor (say notepad) and copy below code and save with extension .au3
(e.g file.au3)

WinWait("[TITLE:Opening ; CLASS:MozillaDialogClass]","", 10)
If WinExists("[TITLE:Opening ; CLASS:MozillaDialogClass]") Then
WinActivate("[TITLE:Opening ; CLASS:MozillaDialogClass]")
Send("{DOWN}")
Sleep(20)
Send("{TAB}")
Sleep(20)
Send("{TAB}")
Sleep(20)
Send("{ENTER}")
EndIf

Step 3: From extracted zip (Step 1) look for folder named: Aut2Exe and open it

Step 4: Click Aut2exe_x64.exe if your OS is 64 bit otherwise click Aut2exe.exe

Step 5: Browse/Locate file created in Step2. (file saved as extension .a3) AND Choose Destination (.exe/.a3x) and select .exe option (say file.exe) AND then Click convert

Step 6: include this file.exe in your project folder and use it as per your requirement using below code (as it is in Eclipse):

driver.dwonload().click(); // it can be something else as per your flow
Runtime.getRuntime().exec("C:/*path_to_your_EXE_file(selected in step 6))*/file.exe");
eaccmk
  • 366
  • 2
  • 6