python+selenium:判断弹出框是否出现,如果弹出框出现,则点击关闭按钮,如果没有弹框,就继续下一步的操作
self.driver.find_element(By.CSS_SELECTOR,"xxx").click()
first_handle = self.driver.current_window_handle
#如果出现弹框
try:
first_handle = self.driver.current_window_handle
time.sleep(1)
handles = self.driver.window_handles
time.sleep(3)
for i in handles:
if i != first_handle:
self.driver.close() # 关闭当前窗口
self.driver.switch_to.window(i)
self.driver.find_element_by_class_name("xxx").click()
time.sleep(3)
except:
#如果没有出现弹框
time.sleep(2)
#有弹框关闭之后/或者无弹框之后的操作:也算是公共操作吧
self.driver.find_element...