How to disable/enable "Save as" dialog in Selenium Chromedriver? - python

I googled this question and found a lot of answers, still non of the solutions worked for me.
So my question is: how to Enable "Save as" dialog for Selenium Chromedriver? I want to see this dialog when I download a file.
Good to see how to do it in Python but any other languages are good too.
I've tried to add the next line "{download.prompt_for_download': 'true'}" to arguments and to experimental options:
chrome_options.add_experimental_option("prefs"{'download.prompt_for_download': 'true'})
chrome_options.add_argument({'download.prompt_for_download': 'true'})
The first one didn't give any result, the second one caused code error.
I also tried "saveas-menu-label" and got no result.

If you want to just see the dialog when you download the file, open settings and select "Ask where to save each file before downloading".
If your configuration requires to do this from the code, try this solution:
"How to make the settings for Download in Chrome when launched using Chromedriver?"

There is a syntax error in your code as true/false are supposed to be given in boolean and not string. So in python, it could be so
chrome_options.add_experimental_option("prefs",{'download.prompt_for_download': True})
Hope this helps you out.

Related

Question about using selenium+pyautogui to upload file

Recently I need to upload my dataset on a website, the first part works fine on selenium.
However, after I click the 'upload' button, a windows file broswer pops-up.
And after searching online, I finally make it work by using pyautogui, code:
pyautogui.write('K:\Github\###\data\global\Global_PM_corT.csv') # enter file with path
pyautogui.press('enter') # click ok
My question is, I want to use above process on Github Action.
So, i changed:
'K:\Github\###\data\global\Global_PM_corT.csv'
to:
'./data/global/Global_PM_corT.csv'
and it's not working.
I think there is some error when using relative path with pyautogui.
So, i was wondering if someone could help me achieve this? Thanks in advance:)
P.S.
its working right now.
I first read the dataset by
pd.read_csv('path')
then write it on github action
to_csv('C:\\Global_PM_corT.csv')
then I can use absolute path with pyautogui:)
However, I still want to know if a better way to achieve this
Please try this:
'./data/global/Global_PM_corT.csv'

Python3.x, How to focus on newly opened webpage

Ok, here's the deal. I use:
driver = webdriver.Firefox(firefox_profile=profile)
driver.get = ('http://google.com')
to open a new webpage. After opening the new webpage, I use pyautogui to do some clicking and scrolling and whatnot, and then close the browser, and then do the same thing over again as the program iterates through a list of proxies. But when the webpage opens, it isn't in focus. I can see it and see that it opened, but it's not in focus.
So I originally implemented a simple click command on the webpage, and that usually focuses the new browser. But after the first browser is opened and closed, the next browser that is opened using a new proxy from the list isn't in focus, and for whatever reason, the click doesn't bring it into focus. So because the browser isn't in focus, my scroll code doesn't happen, it just acts like it skips over it. So I guess my question is, is there some command I can use to make my program focus on the new browser? I've searched extensively on here for the answer to my question and have tried a bunch of different things, but none have worked. Most answers on here pertain to newly opened tabs, and that isn't my issue. Any helpful comments or ideas would be much appreciated. I'm using Mac OS X, and the most recent version of python. Thank you.
Scratch the whole click on the dock icon thing, after extensive testing, that didn't work consistently. However, what did actually work consistently was using pyautogui to click on the "new tab" button at the top, and then close tab, and boom, webpage focused.

windows warning starting chrome with Selenium Python

I've just started coding in python with selenium, but here is my problem :
When I start Chrome with selenium,I ve a warning message from windows (my computer is in french, but it ask if I want to reset my setting). And I would like to close that tab, to make my program continue on another tab.
But it seems impossible to close it. I tried :
the Alert class of selenium, did nothing, the code just go through it
Moving mouse to the cross, or to "dismiss" (with selenium obviously) just the same
Switching tab, and close it (with selenium's control command), it doesnt change anything
Open Chrome with my usual settings (still from selenium), with that mode i can t open my url anymore, but i dont have the warning message too ...
I would be so grateful if someone had an idea, or have already had that problem. In fact, i would just want to dismiss it, to make my program run, without having to close the window at the start.
Apologize me for my english, that's not my native language, but I swear that I do my best :/
Thank you very much to all, feel free to ask me more informations :)
AR
Edit: I can't forbid you to put a -1, but try to explain me in which way it deserves it, then, I'll try to correct me, or to give more details.
Here the solution, even if I would prefer a patch from Selenium, because to my mind, that is a trouble comming from Selenium/python + windows + chrome combination : I ve created a new profile on chrome, add the X.add_argument("user-data-dir=...) It doesn't open my profile (I've maybe done a mistake in the path to the folder) BUT the warning message disappears with that line, all I wanted, then thank you very much Bill Bell ;)

webbrowser python open new url in same tab. Does this ever work?

It seems to me that Python's webbrowser 'new=0' functionality (see here), which opens a new url in the same tab or window, is not ever working.
The documentation uses dodgy language like "if possible" to mask this problem.
Has anyone seen any success with this functionality in the webbrowser module? Are there any known workarounds to achieve this functionality?
I have tried setting the webbrowser with
webbrowser.get(TYPE)
before continuing with opening urls. I have also tried using a slew of different browsers, however still have seen no success. Is this just not possible anymore? Should I just use selenium?
Among others, I have checked out this post from 7 years ago. I am hoping things have changed since then and people have found a way around this.
Any help or insight is much appreciated.
I'm using MacOS and Chrome and ran into this. I noticed that for MacOS, webbrowser.py just builds a little applescript and opens the URL with the 'open location' command, e.g.,
script = 'open location "%s"' % url.replace('"', '%22')
The built-in 'open location' command doesn't appear to support opening a url in an existing tab
I searched around in applescript examples and found that you can use commands from the Chrome applescript dictionary to open URL in the active tab like so:
tell application "Google Chrome"
tell front window
set URL of active tab to "www.google.com"
end tell
end tell
I only needed to do this on a local project, so I just went with the dirty fix of overwriting the string in the 'script' variable to force it to use the latter format, like so:
# script = 'open location "%s"' % url.replace('"', '%22')
script = '''tell application "Google Chrome"
tell front window
set URL of active tab to "%s"
end tell
end tell ''' % url.replace('"', '%22')

Python selenium: Firefox auto download everything

I am trying to download a csv file from a website and no matter how many MIME's I try for both saving and opening, the dialog box still appears. Is there a way to cover all MIME's at once or anything that might pop up?
Right now I'm using:
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/html")
fp.set_preference("browser.helperApps.neverAsk.openFile","text/html")
And other variations including a ton more MIME's. How can I auto download regardless of the MIME?
I solved my question with this:
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")
fp.set_preference("browser.helperApps.neverAsk.openFile","application/octet-stream")
I had multiple preference statements, and I think that is what the problem was. Now I only have this one active and don't have any problems. Hope this can help someone else.

Categories