先登录QQ首页登录
然后再跳转到测试页面,这时测试页面就有登录态了
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://www.qq.com")
driver.maximize_window()
#获取qq登录态
driver.find_element_by_xpath(".//*[@id='loginGrayLayout']").click()
#登录表单在页面的框架中,所以要切换到该框架
driver.switch_to_frame("login_frame")
driver.find_element_by_id("switcher_plogin").click()
driver.find_element_by_id("u").clear()
driver.find_element_by_id("u").send_keys("QQ帐号")
driver.find_element_by_id("p").clear()
driver.find_element_by_id("p").send_keys("QQ密码")
driver.find_element_by_id("login_button").click()
#切换到测试页面
driver.get("http://beta.card.automall.qq.com/web")
#获取当前窗口
sreach_windows = driver.current_window_handle
driver.find_element_by_xpath(".//*[@id='content']/div[2]/div[1]/ul/a[1]/li/h2").click()
#获取当前所有窗口
all_handles = driver.window_handles
#窗口切换
for handle in all_handles:
if handle !=sreach_windows:
driver.switch_to.window(handle)
driver.find_element_by_xpath(".//*[@id='content']/div/div[1]/div[2]/ul/li[4]/div/div[1]/span").click()
driver.find_element_by_xpath(".//*[@id='pick-store']/ul[1]/li/a").click()
driver.find_element_by_xpath(".//*[@id='act_buy_btn']/div/a").click()