1 """通过系统自带的弹窗处理办法""" 2 try: 3 driver.switch_to.alert.accept() 4 except EC.NoAlertPresentException: 5 pass 6 7 8 """通过查找关键字,有两种""" 9 #第一种(传入的关键字参数如果不止一个也可以写一个列表,循环遍历判断) 10 try: 11 popup = driver.find_element_by_xpath("//*[@text='始终允许']") 12 if popup: 13 popup.click() 14 except: 15 pass 16 17 sure_text = ['确定','確定','OK','ok','install'] 18 for sure in sure_text: 19 try: 20 popup = driver.find_element_by_xpath("//*[@text='%s']"%sure) 21 if popup: 22 popup.click()
break(或者加else) 23 except: 24 pass 25 26 #第二种 27 for i in range(3): 28 loc = ('xpath',"//*[@text='始终允许']") 29 try: 30 e=WebDriverWait(driver,2,0.5,).until(EC.presence_of_element_located(loc)) 31 e.click() 32 except: 33 pass
如有新的方法后续会补充