Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

Selenium+Chrome模拟登录 #128

Open Qingquan-Li opened 5 years ago

Qingquan-Li commented 5 years ago

开发环境


一、Selenium直接操作Chrome浏览器

直接调起 Chrome 浏览器执行自动化操作(不是操作无头的Chrome)。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import time

# 调用环境变量指定的ChromeDriver创建浏览器对象。这里因为没有在环境变量配置指定ChromeDriver,需指定ChromeDriver位置。
# driver = webdriver.Chrome(executable_path='/Users/fatli/python/chromedriver')  # 绝对路径
driver = webdriver.Chrome(executable_path='./chromedriver')  # 相对路径

try:
    driver.get('<login_url>')
except TimeoutException:
    print('访问登录页面超时')

username_field = driver.find_element_by_id('username')
password_field = driver.find_element_by_id('password')
# submit_button = driver.find_element_by_tag_name('button')
submit_button = driver.find_element_by_css_selector('button.ng-binding')

# 清空输入框(二次登录时该页面会记住用户名,如果不清除,则会在记住的用户名后追加用户名)
username_field.clear()

username_field.send_keys('<username>')
password_field.send_keys('<password>')
submit_button.click()
time.sleep(5)
# driver.close() # Closes the current window.
driver.quit() # Quits the driver and closes every associated window.


二、Selenium操作headless-chrome

关于无头Chrome:https://developers.google.com/web/updates/2017/04/headless-chrome

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time

# 操作无头chrome浏览器
# from selenium.webdriver.chrome.options import Options
# chrome_options = Options()
options = webdriver.ChromeOptions()
options.add_argument('--headless')  # 启动无头模式
# 调用环境变量指定的ChromeDriver创建浏览器对象。这里因为没有在环境变量配置指定ChromeDriver,需指定chromedriver位置。
driver = webdriver.Chrome(options=options, executable_path='./chromedriver')

try:
    driver.get('<login_url>')
    # 截屏(登录前)
    driver.get_screenshot_as_file('./01before_login.png')
except TimeoutException:
    print('访问登录页面超时')

try:
    username_field = driver.find_element_by_id('username')
    password_field = driver.find_element_by_id('password')
    # submit_button = driver.find_element_by_tag_name('button')
    submit_button = driver.find_element_by_css_selector('button.ng-binding')
except NoSuchElementException:
    print('No Such Element: 登录表单的某些元素')

# 清空输入框(二次登录时该页面会记住用户名,如果不清除,则会在记住的用户名后追加用户名)
username_field.clear()

username_field.send_keys('<username>')
password_field.send_keys('<password>')
# 截屏(登录中)
driver.get_screenshot_as_file('./02logging_in.png')
# 点击提交登录按钮失败:“Element is not interactable”,参考:
# stackoverflow.com/questions/52859981/selenium-generating-error-element-is-not-interactable
submit_button.click()

try:
    # WebDriverWait和expected_conditions两个模块组合起来构成Selenium的显式等待
    # github.com/FatliTalk/blog/issues/125
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'visit-list-talbe'))
    )
except Exception as e:
    print('抛出异常:')
    print(e)
finally:
    # 截屏(登录后)
    driver.get_screenshot_as_file('./03after_login.png')
    print(driver.find_element_by_tag_name('tbody').text)
    # print(driver.find_element(By.TAG_NAME, 'tbody').text) # 作用同上
    # driver.close() # Closes the current window.
    driver.quit() # Quits the driver and closes every associated window.