案例:selenium实现登录处理弹窗
func.py
https://www.cnblogs.com/andy9468/p/10899508.html
main.py中
# 导入webdriver import os import time from lxml import etree from selenium import webdriver from func import base642str, str2base64 # 将mm隐匿化 # import sys # print(str2base64("123456")) # str1_base64="MTIzNDU2" # print(base642str(str1_base64)) # sys.exit() print('000-正在启用selenium...') # 调用环境变量指定的PhantomJS浏览器创建浏览器对象 driver = webdriver.Chrome(r'D:\xxx\chromedriver.exe') print('000-OK') url = 'https://www.xxx.com/login.jsp' print('111-selenium正在请求页面:%s' % url) driver.get(url) # get方法请求页面,获取响应 print('111-请求OK') print("打印标题") print(driver.title) # print("打印登录页面的代码") # html = driver.page_source # print(type(html)) # print(html) print('222-selenium正在填写表单...') time.sleep(1) account = "abcdef" pwd = "MTIzNDU2" input_box1 = driver.find_element_by_xpath("//input[@class='input_username']") input_box1.send_keys(account) time.sleep(0.5) input_box2 = driver.find_element_by_xpath("//input[@class='input_password']") input_box2.send_keys(base642str(pwd)) print('222-填写表单OK') time.sleep(2) print('333-selenium提交表单...') one_click = driver.find_element_by_xpath("//div[@class='button_div_c']") one_click.click() print('333-登录成功...') # 浏览器跳转新窗口后,selenium绑定新窗口 print('444-页面跳转后重新绑定selenium.') time.sleep(3) search_window = driver.current_window_handle # 此行代码用来定位当前页面 html = driver.page_source print("444-打印标题") print(driver.title) print("555-正在打开弹窗黄页...") workers = driver.find_element_by_xpath("//div[@class='header_menu_item_body']/div[7]") # workers = driver.find_element_by_id("header_menu_item_body") print(workers) workers.click() time.sleep(3) # 浏览器弹出新窗口后,selenium绑定新窗口 windows = driver.current_window_handle # 定位当前页面句柄 all_handles = driver.window_handles # 获取全部页面句柄 for handle in all_handles: # 遍历全部页面句柄 if handle != windows: # 判断条件 driver.switch_to.window(handle) # 切换到新页面 print("555-打印标题") print(driver.title) print('666-selenium正在填写表单...') bd_searchbox = driver.find_element_by_xpath("//input[@class='input_username']") bd_searchbox.send_keys(account) time.sleep(0.5) bd_searchbox = driver.find_element_by_xpath("//input[@class='input_password']") bd_searchbox.send_keys(base642str(pwd)) print('666-填写表单OK') time.sleep(3) print('777-selenium提交表单...') baidu_click = driver.find_element_by_xpath("//div[@class='login_button_div_c']") baidu_click.click() print('777-登录成功2...')