使用selenium模拟浏览器登录csdn牛刀小试

使用selenium模拟浏览器操作的方式登录CSDN,还好前几次操作没有遇到验证码问题,还算顺利。贴出源代码如下:

from selenium import webdriver
import time

driver = webdriver.Firefox()
try:
    driver.get("https://passport.csdn.net/account/login")
    # data = driver.title
    # print(data)
    # print(driver.page_source)

    log_by_account_link = driver.find_element_by_link_text('账号登录')
    print(log_by_account_link)
    log_by_account_link.click()

    username = driver.find_element_by_id('username')
    password = driver.find_element_by_id('password')
    log_button = driver.find_element_by_xpath("//form[@id='fm1']/input[@class='logging']")

    print(username)
    print(password)
    print(log_button)
    '''
    这一句sleep如果不加,则会提示Message: Element  could not be scrolled into view
    原因是页面还没加载完成,屏幕上没有发现username这个输入框。
    '''
    time.sleep(8)
    username.clear()
    username.send_keys('你的用户名')
    password.clear()
    password.send_keys('你的密码')
    time.sleep(5)
    log_button.click()

except Exception as e:
    print(e)
finally:
    driver.quit()
    print('OK')

你可能感兴趣的:(使用selenium模拟浏览器登录csdn牛刀小试)