使用selenium实现某网页游戏的自动登陆,flash加载和嵌套页面的切换

import urllib.request
from selenium import webdriver
from http.cookiejar import CookieJar, LWPCookieJar
import time

from selenium.webdriver.chrome.options import Options

headers = {

'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'cna=eO5+Ex53RFgCAXLyGpGP5FSX; UM_distinctid=16390c8b7478a3-033a79337c57c3-336c7706-13c680-16390c8b748d0e',
'Host': 's95.cnzz.com',
'Pragma': 'no-cache',
'Referer': 'http://www.501wan.com/start/2302.html',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}

sgwy_url = 'http://www.501wan.com/passport/sign/index.html?tourl=http%3A%2F%2Fwww.501wan.com%2Fstart%2F2302.html'

chromeOpitons = Options()

prefs= {
    "profile.managed_default_content_settings.images":1,
    "profile.content_settings.plugin_whitelist.adobe-flash-player":1,
    "profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player":1,

}


chromeOpitons.add_experimental_option('prefs', prefs)


driver = webdriver.Chrome('./chromedriver', chrome_options=chromeOpitons)

cookie = CookieJar()

handler = urllib.request.HTTPCookieProcessor(cookie)

opener = urllib.request.build_opener(handler)

request = urllib.request.Request(url=sgwy_url, headers=headers)

opener.open(request)

driver.get(sgwy_url)
# driver.add_cookie(cookie)
p_username = driver.find_element_by_id('p_username')
p_username.send_keys('用户名')
p_password = driver.find_element_by_id('p_password')
p_password.send_keys('密码')

tologin = driver.find_element_by_id('tologin')
print(tologin)
tologin.click()
iframe = driver.find_element_by_xpath("//iframe[contains(@src,'http://s6.sgwy.h5hz.com/interface/userlogin.php?name=501wan&userid=')]")
# 切换嵌套的页面
driver.switch_to.frame(iframe)

猜你喜欢

转载自blog.csdn.net/weixin_41607151/article/details/80487059
今日推荐