How to click and verify the existence of a pop up (alert)

前端 未结 3 2139
面向向阳花
面向向阳花 2021-01-14 20:53

I\'m working with selenium. while trying to click a button it creates a pop up (alert) and doesn’t return a page object. Because of that I can’t use “click” alone as this me

相关标签:
3条回答
  • 2021-01-14 21:29

    as far as I know you have to use always in alerts

    selenium.get_confirmation()

    from python doc: If an confirmation is generated but you do not consume it with getConfirmation, the next Selenium action will fail.

    0 讨论(0)
  • 2021-01-14 21:31

    I am doing the following to dismiss an Alert (you can adapt to get it to only verify the popup)

    def dismissAlert(): 
        result = None
        try:
            alert = browser.switch_to.alert
            alert.dismiss()
            result = True
        except NoAlertPresentException:    
            result = False
    
        return result
    
    0 讨论(0)
  • 2021-01-14 21:37

    for an alert you will need to use either

    getAlert() call which will click ok on the alert. It will return the text in the alert as well so you can check its the alert that you want.

    so an example would be

    self.assertEqual("An alert",selenium.get_alert());
    
    0 讨论(0)
提交回复
热议问题