python selenium练习

import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


# 实例化driver对象
driver = webdriver.Chrome()

# 窗口最大化
driver.maximize_window()

driver.get('https://weibo.com/')

# 输入账号
username = WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located((By.ID, 'loginname')))
username.send_keys('')
time.sleep(1)

# 输入密码
password = driver.find_element_by_name('password')
password.send_keys('')
time.sleep(1)

# 登录
submit_elements = driver.find_element(By.XPATH, '//*[@class="W_btn_a btn_32px"]')
try:
    webdriver.ActionChains(driver).move_to_element(submit_elements).click().perform()
    time.sleep(2)
    # driver.quit()
    print(driver.page_source)
except:
    print('登录失败,需要验证码')

 

posted @ 2022-04-22 12:40  不会飞的鲨鱼  阅读(68)  评论(0编辑  收藏  举报