I’m currently working on a program that, with the help of the selenium’s webdriver and firefox, crawls it’s way through domains, scraping all visible text in the process. (program written in python)
When selenium opens pages with the javascript window.print() the crawler is currently stopping, waiting for me to manually click “close” on the popup window that appears.
I’ve tried the driver.select_pop_up() function, after delay time, in the hopes of being able to close() the window after selecting it. Couldn’t however select the window.
I’ve been reading up on the issue, and from this selenium FAQ page, I pretty much concluded that I had to do a workaround when I read:
To solve this issue, you may use a workaround (if one exists); otherwise you may have to exclude the test from your automated corpus.
The only solution I see is to not open webpages with links descriptions containing the word “print”, I find this ugly however, and would like to hear if anyone else has a better Idea.
Code that shows an example of my problem:
from selenium import webdriver
import time
skrivutsiden = 'www.alfkvam.no/index.php?id=4849944&cat=159037&printable=1'
vanligside = 'http://www.google.no'
driver = webdriver.Firefox()
driver.get(vanligside)
driver.get(skrivutsiden)
EDIT:
Using the code proposed by prestomanifesto, I actually managed to fire a “print popup window” in an empty firefox window. When the code produces the event I’m trying to avoid, I’m thinking that it may be the wrong code in the first place? Is this a wrongfully drawn conclusion?
The code below produced the popup window:
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.execute_script("window.print() = function() {}")
RE-EDIT:
The reason the above code fires the popup window, is because I got the javascript wrong – I wrote
“window.print() = function() {}”
instead of
“window.print = function() {}”
This didn’t solve the original problem, but it explains the unexpected popup window, introduced in the first edit.
my apologies presto manifesto
RE-EDIT:
Update solutions tried. I found this site, where I saw the following line of code proposed:
((JavascriptExecutor)m_driver).executeScript("window.confirm = function(msg){return
false;};");
Based on that I tried the following to no avail:
driver.execute_script("window.print = function(msg) {return false;};")
I finally found a work around:
I pasted
into the Firefox user profile. Selenium has it’s own default user profile, which you can edit. The file containing the profile is called firefox_profile.py
Please note that this just enables printing without asking the user by way of a pop up window. It works for me, because I do not have a printer attached.
I found these to lines of code in the introduction for the r-kiosk add-on.