0

I can't use alert.accept() since my program will automatically defocus before it can work (it has to check for other things as well).

I don't want any popups at all, but I don't know how to outright disable them, which is important since they prevent me from closing the tab. I haven't been able to find any answers that don't involve alert.accept() and use python.

Can I do this? If not, are there any workarounds for my situation? (If there aren't the program I'm trying to make is impossible.)

  • What do you mean by your program defocused? Do you mean the browser window defocused or the program window defocused? Usually you want the browser in focus. – KTibow Apr 24 '20 at 01:12
  • Is the page using `alert()` or another type of pop-up? – KTibow Apr 24 '20 at 01:55
  • It defocuses from the popup. It clicks away. (which doesn't work when a user does it, but apparently does with selenium?) –  Apr 24 '20 at 02:14
  • I'm not sure. It just won't let me close out the page. –  Apr 24 '20 at 02:15

1 Answers1

0

As this question says, as long as you're okay blocking all alerts, you can just override alert(). Here's an example in Python:

from selenium import webdriver

driver=webdriver.Firefox()
driver.implicitly_wait(3)
driver.get("http://example.com")
js = "window.alert = function() {}"
driver.execute_script(js)

Or because the alert pops up on page close, you could try this:

from selenium import webdriver

driver=webdriver.Firefox()
driver.implicitly_wait(3)
driver.get("http://example.com")
js = "window.onbeforeunload = null"
driver.execute_script(js)
KTibow
  • 495
  • 6
  • 18
  • This doesn't work for some reason. Alerts still show. You gave me an example for Firefox, but I'm using Brave and Chrome so that might be the issue. I tried running it in the console and it doesn't work there either. Thanks for the answer, though! –  Apr 23 '20 at 03:10
  • @AmeliaMayIX Can you try adding a semicolon at the end of the string? It works just fine in my browser console. – KTibow Apr 23 '20 at 15:45
  • I haven't tested this, to tell the truth I'm not sure. Personally I just opened my Firefox developer JS console and typed in that. Can you try making your window visible and opening the developer console? Once it's open, type in window.alert to see if the script ran or not. If it's not a plain `alert()` call, this won't help. – KTibow Apr 24 '20 at 01:11
  • Are you sure this works cross platform? I'm using Chrome and Brave. –  Apr 24 '20 at 01:29
  • I cant call ```alert()``` myself, but the website can. –  Apr 24 '20 at 01:30
  • Then probably they aren't using `alert()`. Can you send me a screenshot? Use an online image uploading service and paste the link. – KTibow Apr 24 '20 at 01:33
  • That probably isn't a standard pop-up. What's wrong with using `alert.accept()` in this case? – KTibow Apr 24 '20 at 13:54
  • ```alert.accept()``` doesn't actually accept the popup since my bot clicks away too fast. –  Apr 24 '20 at 23:50
  • I'm confused. Clicks away from what? You can always use a `time.sleep()`. – KTibow Apr 24 '20 at 23:52
  • It clicks away from the popup. It closes out immediately, which means I don't have time to accept it. –  Apr 25 '20 at 01:01
  • Selenium shouldn't click away from a pop-up. The pop-up closes out or the page closes out? Why do you want to accept it? – KTibow Apr 25 '20 at 01:03
  • The popup closes out. I want to accept it so I can close the tab. –  Apr 25 '20 at 01:50
  • What's wrong with full-out quitting Selenium with `driver.quit()` in this case? And by "the popup closes out" do you mean that if you keep closing the tab it's left open? – KTibow Apr 25 '20 at 13:36
  • I want the user to be able to click the X to close the program. (When the program finds that there are no tabs open it will close everything out). If the user hits the X the popup suddenly appears and then quickly disappears, which means the tab is left open. –  Apr 26 '20 at 02:39
  • @AmeliaMayIX I added an example with `onbeforeunload`. Can you try it? – KTibow Apr 26 '20 at 13:51
  • I tried the edit but it doesn't work either, the popup still shows. I tried combining both of them, but that doesn't work either. –  Apr 29 '20 at 08:24