0

I was coding a script written in Py and Selenium that slides some slide and clicks a button of each slide, but sometimes, when the button is clicked, a popup is displayed, and other times the script should go regularly.

Can anybody explain me how to handle the ‘exception’ and How can write correctly the Code?

I'm sure to use Try and Exception, but I don't know how to recognize the Popup, so every attempt I tried failed.

  try:
      element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, “x”)))
      element.click() #Click Slider Button

      element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, “y”)))
  element.click() #Click arrow to go to next slide

exception that, if popup is displayed, do this code

  except driver.find_element_by_class_name(“z”).is_displayed():
      element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, “f”)))#click to close popup
      element.click() 

      element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, “y”)))
      element.click() #click arrow to go to next slide

1 Answers1

0

Try using some popups blocker extensions. If you use a chrome based navigator than this extension works perfectly and will block all possible popups.

Download the extension with this service and follow this question if you don't know how to use extensions with selenium.

This is not exactly the solution you want but it works.

Yassine Akermi
  • 470
  • 5
  • 11
  • I wouldn't like top use this solution because the button click of the popup is import for the script I'm doing –  Dec 23 '18 at 18:22
  • Could you please elaborate? If you want to do some actions with the popup then this is not a good solution but if you only want to close the popup and continue the task, you should use it. – Yassine Akermi Dec 23 '18 at 20:54