Skip to content Skip to sidebar Skip to footer

Python Click Button On Alert

I am new to python, but need to modify code created by someone else. I am not able to post the full code, but I posted most of it below: from bs4 import BeautifulSoup import dateti

Solution 1:

A couple of points here :

  • switch_to_alert had been deprecated so we have to mandatory use switch_to().alert()
  • Seems to me a purely timing issue. Hence we need to induce ExplicitWait i.e. WebDriverWait with expected_conditions clause set to alert_is_present as follows :

    from selenium.webdriver.support import expected_conditions as EC
    #code block
    self.chrome_session.find_element_by_xpath("//input[@value='Accept']").click()
    WebDriverWait(self.chrome_session, 10).until(EC.alert_is_present)
    self.chrome_session.switch_to().alert().accept()
    print("Accepted work order {0} at {1}.".format(link_text, datetime.datetime.now()))
    

Solution 2:

I dont know if the below code will help you, at one point I faced the same problem and ended up waiting for an alert and then accepting the alert, worked for me.

from selenium.common.exceptions import NoAlertPresentException,

   defwait_for_alert(self):

        for i inrange(50):
            try:
                alert = chrome_session.switch_to.alert
                if alert.text:
                   breakexcept NoAlertPresentException:
              pass
            time.sleep(.25)
        else:
            raise NoAlertPresentException("Alert visibility timed out")
    return alert

wait_for_alert().accept()

Solution 3:

Try The Code below! Working Fine for me!

alert = driver.switch_to.alert #This .alert will work For Pythontry:
   alert.accept() #If you want to Accept the Alertexcept:
   alert.dismiss()  #If  You want to Dismiss the Alert.

Post a Comment for "Python Click Button On Alert"