Python click button on alert

前端 未结 3 486
Happy的楠姐
Happy的楠姐 2020-12-21 14:59

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 Beau         


        
相关标签:
3条回答
  • 2020-12-21 15:34

    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,
    
       def wait_for_alert(self):
    
            for i in range(50):
                try:
                    alert = chrome_session.switch_to.alert
                    if alert.text:
                       break
                except NoAlertPresentException:
                  pass
                time.sleep(.25)
            else:
                raise NoAlertPresentException("Alert visibility timed out")
        return alert
    
    wait_for_alert().accept()
    
    0 讨论(0)
  • 2020-12-21 15:41

    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()))
      
    0 讨论(0)
  • 2020-12-21 15:44

    Try The Code below! Working Fine for me!

    alert = driver.switch_to.alert #This .alert will work For Python
    try:
       alert.accept() #If you want to Accept the Alert
    except:
       alert.dismiss()  #If  You want to Dismiss the Alert.
    
    0 讨论(0)
提交回复
热议问题