문제

I have a webpage that whenever I leave the page, a dialog box comes up asking if I'd "Really like to leave the page", with a 'Leave' or 'Stay options. This box is created by javascript.

I'm currently using Spynner for Python as the browser tool that's going on this page. Spynner has the ability to inject Javascript. This is what I came up with, but doesn't seem to get the job done in Python.

browser.runjs("""window.alert = function() {};""")

I've also tried alternatives such as these, which I've used for previous scripts to inject in to the page, however I get a syntax error when using this that I can't seem to point my finger on.

Injecting Script that DOES work:

browser.runjs("""jQuery(document.elementFromPoint(204,51)).click();""",debug=True)

Injecting Script that DOES NOT work, but need to get working:

browser.runjs("""jQuery(window.alert = function()) {};""")

Any help is greatly appreciated, thank you!

EDIT: Tried giving this a shot. Didn't work either. I'm a bit lost.

Ran This:

browser.runjs("""window.alert = function();""") 

Console said this:

Run Javascript code: window.alert = function(); 
Javascript console (undefined:1): SyntaxError: Parse error
도움이 되었습니까?

해결책

Ended up solving the problem with the following.

def confirmcallback(url, message):
    return True
browser.set_javascript_confirm_callback(confirmcallback)

def cancel_answer(url, message, defaultvalue):
    return
browser.set_javascript_prompt_callback(cancel_answer)

다른 팁

for that code you don't need

jQuery(...)

just add

window.alert = function(){};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top