0

Some website may ask me different questions with pop-up alerts.

I can catch them with Selenium (python). But can not see their text. I tried

an_alert_object = driver.switch_to.alert

and

an_alert_object = selenium.webdriver.common.alert.Alert(driver)

and

an_alert_object = WebDriverWait(driver,10).until(EC.alert_is_present())

I do always avoid NoAlertPresentException

I can do

an_alert_object.accept()

in any way.

But I never can get an_alert_object.text it is always empty. Why? What am I doing wrong ? How can I get the text of an alert (actually it is confirm box)?

C4H7Cl2O4P
  • 11
  • 7
  • the Alert type should have a getText() method... try with an_alert_object.getText(); (and maybe cast it as Alert type... Alert an_alert_object = ...) – pcalkins Dec 20 '21 at 17:43
  • @pcalkins: `.getText` is in `Java bindings`, `.text` is right method for Python. – cruisepandey Dec 20 '21 at 17:58
  • to OP: Show us how does it look like? maybe through screenshot or Weburl, also can you inspect that `confirmed box` ? – cruisepandey Dec 20 '21 at 18:01
  • Both .getText() and .get_text() cause AttributeError: 'Alert' object has no attribute 'getText'/'get_text' python doesn't have them – C4H7Cl2O4P Dec 20 '21 at 20:19
  • I'd be glad to inspect these alerts (confirm boxes) but I don't know how. What would you lke to see with screenshot ? – C4H7Cl2O4P Dec 20 '21 at 20:22

1 Answers1

0

Before you attempt to get the text you need to switch to the Alert first:

alert = driver.switch_to.alert()

Preferably inducing WebDriverWait for the alert_is_present() and extract the text as follows:

alert = WebDriverWait(driver, 5).until(EC.alert_is_present())
time.sleep(3)
alert_text = alert.text

Referances

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352