import asyncio
import time
import random
from pyppeteer import launch # 控制模拟浏览器用
from pyppeteer.dialog import Dialog
from retrying import retry # 设置重试次数用的
js1 = '''() =>{Object.defineProperties(navigator,{webdriver:{get: () => undefined} })}'''
js2 = '''() => {alert ( window.navigator.webdriver )}'''
async def close_dialog(page):
# 点击弹窗
# class*= 模糊匹配
try:
await page.evaluate(
'''var temp=document.querySelector('button[class*="infotip__close"]');if(temp){temp.click()}''')
await page.evaluate(
'''var temp=document.querySelector('span[class="step-close"]');if(temp){temp.click()}''')
await page.evaluate(
'''var temp=document.querySelector("a[class*='dialog-close']");if(temp){temp.click()}''')
await page.evaluate('''document.querySelector("div[role='dialog']").remove()''')
await page.evaluate('''var temp=document.querySelector('button[class*="close"]');if(temp){rs.click()}''')
await page.evaluate('''var rs=document.querySelector('a[title="Close"]');if(rs){rs.click()}''')
await page.evaluate(
'''var heddens=document.querySelector("div[style*='overflow:hidden']");if(heddens){heddens.remove()}''')
a_tags = await page.xpath('//button[contains(@class,"close") or contains(@id,"close") or contains(@aria-label,"close")]')
a_tags1 = await page.xpath('//a[contains(@class,"close") or contains(@id,"close") or contains(@aria-label,"close") or contains(@title,"Close")]')
for a in a_tags + a_tags1:
a.click()
except:
pass
async def main(url): # 定义main协程函数,
#插件路径
chrome_extension = r'C:\Users\Administrator.USER-20190313RI\AppData\Local\Chromium\User Data\Default\Extensions\dbclpoekepcmadpkeaelmhiheolhjflj\0.2.7_0'
args = ['--no-sandbox',
'--disable-gpu',
'--log-level=3', #日志等级
'--disable-infobars', # 关闭提示
'--window-size={},{}'.format(1080,950),
# "--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
'--load-extension={}'.format(chrome_extension), #加载扩展插件
'--disable-extensions-except={}'.format(chrome_extension),
]
if proxy:
ip=re.findall('(\d+\.\d+\.\d+\.\d+:\d+)',proxy)
if len(ip)>0:
args.append("--proxy-server=http://{}".format(proxy))
exepath = r'D:\Program_Files\chrome-win32\chrome.exe' #chromium浏览器路径
# dumpio:True 浏览器就不会卡住了
#浏览器启动参数
params={'executablePath': exepath,
"userDataDir": r"F:\temporary",
'headless': False,
'args':args,
'dumpio':True}
browser = await launch(params) # 启动pyppeteer 属于内存中实现交互的模拟器
page = await browser.newPage() # 启动个新的浏览器页面
await page.setUserAgent(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36')
#设置页面超时
page.setDefaultNavigationTimeout(1000*60) #60s
#启用js
await page.setJavaScriptEnabled(True)
# 启用拦截器
# await page.setRequestInterception(True)
await page.evaluate(js1)
await page.evaluate(js2)
await page.setViewport({'width':1300,'height':868}) #设置界面
page.on('dialog', lambda dialog: asyncio.ensure_future(handle_dialog(page,dialog)))
await page.goto(url) # 访问登录页面
await asyncio.sleep(5)
# 关闭弹窗
await close_dialog(page)
await browser.close()
async def handle_dialog(page,dialog: Dialog):
print(dialog.message)#打印出弹框的信息
print(dialog.type)#打印出弹框的类型,是alert、confirm、prompt哪种
# print(dialog.defaultValue())#打印出默认的值只有prompt弹框才有
await page.waitFor(2000)#特意加两秒等可以看到弹框出现后取消
await dialog.dismiss()
# await dialog.accept(‘000’) #可以给弹窗设置默认值
if __name__ == '__main__':
url = 'https://www.baidu.com'
loop = asyncio.get_event_loop()
m = main(url)
loop.run_until_complete(m)
pyppeteer dialog点击弹窗
于 2020-05-17 21:48:20 首次发布