I am trying to use Selenium
in Python
to save webpages on MacOS Firefox
.
So far, I have managed to click COMMAND + S
to pop up the SAVE AS window
. However,
I don't know how to:
- change the directory of the file,
- change the name of the file, and
- click the SAVE AS button.
Could someone help?
Below is the code I have use to click COMMAND + S
:
ActionChains(browser).key_down(Keys.COMMAND).send_keys("s").key_up(Keys.COMMAND).perform()
Besides, the reason for me to use this method is that I encounter Unicode Encode Error when I :-
- write the page_source to a html file and
- store scrapped information to a csv file.
Write to a html file:
file_object = open(completeName, "w")
html = browser.page_source
file_object.write(html)
file_object.close()
Write to a csv file:
csv_file_write.writerow(to_write)
Error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in position 1: ordinal not in range(128)
You cannot interact with system dialogs like save file dialog. If you want to save the page html you can do something like this:
This is a complete, working example of the answer RemcoW provided:
You first have to install a webdriver, e.g.
pip install selenium chromedriver_installer
.What you are trying to achieve is impossible to do with Selenium. The dialog that opens is not something Selenium can interact with.
The closes thing you could do is collect the
page_source
which gives you the entire HTML of a single page and save this to a file.If you really need to save the entire website you should look into using a tool like AutoIT. This will make it possible to interact with the save dialog.
you can achieve this with pyautogui library but if you have to save multiple pages in a loop, you can't execute any other tasks on the screen.