最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - How to check if an alert is present on a page with JavaScript or Selenium - Stack Overflow

matteradmin7PV0评论

I am making a bot, and this bot is searching constantly but some times when it searches, an alert appears asking the user for access to their location. How do i check if this alert is present or better yet disable it?

So far i have tried to disable stuff through about:config:

    var prefs = Components.classes["@mozilla/preferences- 
       service;1"].getService(Components.interfaces.nsIPrefBranch);

    prefs.setIntPref("network.cookie.cookieBehavior",  4);

    prefs.setBoolPref("privacy.donottrackheader.enabled", true); 

    prefs.setIntPref("permissions.default.geo", 2);

But alerts still pop up. I also tried making a new firefox profile with pre installed add-ons just to block dialog boxes and pop ups, but it still gives me dialog boxes.

I really am stumped this time. If you know how i can check if a page is alerting something with JS or Selenium python then please tell me cause at this point, that would help me a lot. If you know how to disable javascript alerts then that would be awesome.

I am making a bot, and this bot is searching constantly but some times when it searches, an alert appears asking the user for access to their location. How do i check if this alert is present or better yet disable it?

So far i have tried to disable stuff through about:config:

    var prefs = Components.classes["@mozilla/preferences- 
       service;1"].getService(Components.interfaces.nsIPrefBranch);

    prefs.setIntPref("network.cookie.cookieBehavior",  4);

    prefs.setBoolPref("privacy.donottrackheader.enabled", true); 

    prefs.setIntPref("permissions.default.geo", 2);

But alerts still pop up. I also tried making a new firefox profile with pre installed add-ons just to block dialog boxes and pop ups, but it still gives me dialog boxes.

I really am stumped this time. If you know how i can check if a page is alerting something with JS or Selenium python then please tell me cause at this point, that would help me a lot. If you know how to disable javascript alerts then that would be awesome.

Share Improve this question asked Mar 23, 2019 at 17:46 SuperSimplePimpleDimpleSuperSimplePimpleDimple 5661 gold badge4 silver badges20 bronze badges 4
  • This might help, possible duplicate stackoverflow./questions/38767551/… – David Silveiro Commented Mar 23, 2019 at 19:06
  • @David it is actually an alert not a pop-up. If it was a pop up my add-ons would have blocked it. – SuperSimplePimpleDimple Commented Mar 23, 2019 at 19:07
  • Are you using chrome and firefox? – Pritam Maske Commented Mar 23, 2019 at 19:46
  • @PritamMaske Firefox. – SuperSimplePimpleDimple Commented Mar 23, 2019 at 23:51
Add a ment  | 

3 Answers 3

Reset to default 3

You can disable alerts with:

window.alert = () => false

from python that's:

driver.execute_script("window.alert = () => false")

Use :

prefs.setIntPref("permissions.default.desktop-notification",  1);

Assume one is present on the exception of a non located element within a try/catch block, then try to use

driver.switchTo().alert().dismiss();

to proceed with the entended behavior :)

Post a comment

comment list (0)

  1. No comments so far