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 = ''
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'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;};")
Share
Improve this question
edited Aug 24, 2012 at 7:58
Dan Puzey
34.2k4 gold badges76 silver badges98 bronze badges
asked Nov 29, 2011 at 21:46
RookieRookie
1,6005 gold badges21 silver badges35 bronze badges
2
- While i'm not sure why it's not working, I can tell you that your code is calling window.print() and then ignoring the rest of the code. The point of "window.print = function() {}" is to make later window.print() calls actually call "function()" which does nothing {}. Hope that makes some sense – prestomanifesto Commented Dec 1, 2011 at 8:36
- 1 I understood the intended purpose behind your answer, That's why I was so surprised by the firing of the popup window - turns out I got the javascript wrong though (se edit in post). My apologies for that! – Rookie Commented Dec 1, 2011 at 9:24
2 Answers
Reset to default 5I finally found a work around:
I pasted
"print.always_print_silent": "true",
"print.show_print_progress": "false",
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.
You could try the workaround that used to exist for alert
messages before Selenium was able to support them. Basically you want to override the print
function so it does nothing.
So you would do something like this at the beginning of your tests:
driver.execute_script("window.print = function() {}");
(Sorry if the code is not correct, I don't use python very often).
This way once the page calls window.print()
, nothing will happen and your test can continue uninterrupted.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208426a4616702.html
评论列表(0条)