1

In brief

Today I cannot get my code here 00 01 works to get javascript alert() box from Python with Selenium WebDriver.

In full

My google search on the topic gave me many results e.g. this one using EC.alert_is_present() and driver.switch_to_alert()

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("...some page...")
WebDriverWait(driver, 5).until(EC.alert_is_present())
driver.switch_to_alert().accept()

I follow that and write a simple code on my github here 00 01 trying to access the alert object from Python code. Though I failed with the timeout exception as below i.e. the webdriver cannot get the alert() box object.

Traceback (most recent call last):
  File "util/sandbox/my_sandbox/test_alert_box.py", line 49, in <module>
    WebDriverWait(driver,10).until(EC.alert_is_present()) #TODO Why alert box not available? We got error here
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

So if you know how to get it work, please share. Thank you!

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
  • I haven't worked in python and this is just a thought. Can you try executing the webdriver wait inside try/catch block? Also try using implicit wait after initiating the webdriver-->driver.implicitly_wait(10)#seconds – santhosh kumar Jun 20 '17 at 11:06
  • Thanks @santhoshkumar. May you clarify how a try/catch can help - I think it just turn off the exception but the fact remains that we cannot get the alert() box. – Nam G VU Jun 20 '17 at 11:24
  • I have tried `driver.implicitly_wait(10)` and still get the timeout exception. – Nam G VU Jun 20 '17 at 11:27
  • i agree with your question for try catch block. It's weird i know but sometime it worked for me. Again this is just a thought to try. – santhosh kumar Jun 20 '17 at 11:47
  • It is working fine on my machine. To check, alert is coming or not, add some using time.sleep(10) after clicking on try it button. I am able to see the alert dialog. – Murthi Jun 20 '17 at 13:32
  • Wow really wait for 10 seconds @@ I will try though – Nam G VU Jun 20 '17 at 13:38
  • @Murthi I tried time.sleep(10) and timeout still occurred. – Nam G VU Jun 21 '17 at 04:45
  • @Murthi I also tested on simpler test and failed i.e. the alert() box show right when the page is loaded at https://github.com/namgivu/automation-testing/blob/master/util/sandbox/my_sandbox/test_alert_box_00.py – Nam G VU Jun 21 '17 at 04:48
  • Is alert pops up? If not then problem with button click. – Murthi Jun 21 '17 at 05:12
  • If we run the html file manually, all alert boxes are working fine. They just don't work when executed inside webdriver – Nam G VU Jun 21 '17 at 07:58

0 Answers0