Skip to content

Instantly share code, notes, and snippets.

@1600
Last active February 28, 2017 08:10
Show Gist options
  • Save 1600/4d0278344f4ca6c1fbd6f5ad60f09cdf to your computer and use it in GitHub Desktop.
Save 1600/4d0278344f4ca6c1fbd6f5ad60f09cdf to your computer and use it in GitHub Desktop.
phantomjs has a problem with site having onbeforeunload
#! -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
d=webdriver.PhantomJS(service_args=['--load-images=no','--disk-cache=no'])
#d.implicitly_wait(20)
WebDriverWait(d,10)
d.set_page_load_timeout(20)
def confirmation(s):
js_confirm = 'window.confirm = function(){return true;}'
s.execute_script( js_confirm )
#self.find_by_id( 'submit' ).click()
s.execute_script( 'return window.confirm' ) # trigger the injected js that returns true (virtually click OK)
url_list=["http://www.baidu.com/",
"http://www.sitewithonbeforeunload.com/",
"http://bing.com/",
"http://www.google.com/",
"http://www.abc.com/",
"http://www.iqiyi.com/",
]
for i in url_list:
d.get(i)
if "onbeforeunload" in d.page_source:
print "discovered [onbeforeunload]"
confirmation(d)
print d.current_url
'''
sample hidden script source:
<head><meta name="GENERATOR" content="MSHTML 8.00.7601.18448"><script language="javascript">alert('something')</script></head>
<BODY onbeforeunload="return('leaving this page?')" style="margin:0px;"><iframe border="0" name="lantk" width="0" height="0" allowtransparency="" scrollbars="yes" frameborder="0"></iframe>
(without an ending </body> tag)
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment