案例: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...')

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM