python selenium使用

安装selenium

#Python
pip install selenium

#Anaconda3
conda install selenium

下载浏览器版本对应的驱动文件

将下载的文件解压到Python解释器对应的目录下

常用操作

from selenium import webdriver
import time

url = "https://www.qy1.xyz/auth/login"
browser = webdriver.Chrome()
browser.get(url)

# 点击网页的登陆按钮
# time.sleep(3)
# browser.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/p[2]').click()

account = 'xxxx'
password = 'xxxx'

# try:
browser.find_element_by_xpath("//input[@name=\"email\"]").send_keys(account)
browser.find_element_by_xpath("//input[@name=\"password\"]").send_keys(password)
browser.find_element_by_xpath("//button[@type=\"submit\"]").click()
# browser.find_element_by_id("email").send_keys(account)
# browser.find_element_by_id("password").send_keys(password)
# browser.find_element_by_xpath('/html/body/div[1]/section/div/div/div/div[2]/form/div/div[5]/button').click()
# except:
#     browser.find_element_by_id("email").send_keys(account)
#     browser.find_element_by_id("password").send_keys(password)
#     browser.find_element_by_xpath ('/html/body/div[1]/section/div/div/div/div[2]/form/div/div[5]/button/font/font').click()

time.sleep(5)

browser.find_element_by_xpath("//button[contains(text(), '已读')]").click()
# browser.find_element_by_xpath("//button[@type=\"button\"], '已读')]").click()
# browser.find_element_by_xpath('/html/body/div[2]/div/div/div[3]/button').click()

browser.find_element_by_xpath('/html/body/div[1]/div/div[2]/aside/ul/li[8]/a').click()

# 滚屏到指定的位置
browser.execute_script('window.scrollTo(0,2000)')

# 截屏
browser.save_screenshot('some.png')

# 关闭浏览器(不一定能关掉)
browser.close()

猜你喜欢

转载自www.cnblogs.com/s3320/p/12403140.html