selenium在Pycharm中结合python的基本使用、交互、无界面访问

下载

下载与浏览器匹配的浏览器驱动文件,这里一定注意的是,要选择和浏览器版本号相同的驱动程序,否则后面会有很多问题。

(1)浏览器(以google为例)版本号的查询:

我这里的版本号是124版本,所以要下载对应版本号的驱动文件

1.ChromeDriver 国内下载地址:Get WebDriver - Download WebDriver ChromeDriver

2.版本号在114之前的选择如下地址:chromedriver.storage.googleapis.com/index.html

安装selenium

下载selenium包:

python 复制代码
pip install selenium

建议安装较低版本的selenium,这里建议安装4.5.0版本,否则可能会出现闪退问题

python 复制代码
pip install selenium==4.5.0

selenium入门

(1)将下载的chromedriver包解压得到chromedriver.exe,并放到要使用的当前目录下:

(2)在所需要使用的文件中导入selenium

python 复制代码
# (1) 导入selenium
from selenium import webdriver

(3)创建浏览器操作对象

python 复制代码
# (2) 创建浏览器操作对象
browser = webdriver.Chrome()

(4) 访问网站

python 复制代码
# 访问网址
url = 'https://www.jd.com/'    #以访问京东为例
browser.get(url)

(5) page_source获取网页源码

python 复制代码
# 获取网网页源码 page_source获取网页源码
content = browser.page_source
print(content)

技元素定位方法

需要先导入By

python 复制代码
from selenium.webdriver.common.by import By

(1)使用id来找到对象

python 复制代码
button = driver.find_element(By.ID, 'su')
print(button)

(2)根据xpath语句来获取对象

python 复制代码
button = driver.find_element(By.XPATH, '//input[@id="su"]')
print(button)

(3)根据bs4表示语句来获取对象

python 复制代码
button = driver.find_element(By.CSS_SELECTOR, '#su')
print(button)

(4)根据标签名字来获取对象

python 复制代码
button = driver.find_element(By.TAG_NAME, 'input')
print(button)

(5)根据标签名字来获取对象[返回列表,多个]

python 复制代码
button = driver.find_elements(By.TAG_NAME, 'input')
print(button)

(6)链接文本

python 复制代码
button = driver.find_elements(By.LINK_TEXT, '新闻')
print(button)

交互

(1)在文本框中输入内容【send_keys】

python 复制代码
# 获取文本框对象
input  = broswer.find_element(By.ID, 'kw')
# 在文本框中输入周杰伦
content = input.send_keys('袁隆平')

(2)点击按钮

python 复制代码
# 获取百度一下的按钮
baiduyixia  = broswer.find_element(By.ID, 'su')
# 点击一下按钮
baiduyixia.click()

(3)滑到底部[执行脚本]

python 复制代码
js_bottom = 'document.documentElement.scrollTop=100000'
broswer.execute_script(js_bottom)

(4)获取下一页按钮并执行点击

python 复制代码
# 创建浏览器对象
broswer = webdriver.Chrome()
broswer.forward()  #前进

(5)回退到上一页

python 复制代码
broswer = webdriver.Chrome()
broswer.back()  #后退

(6)关闭浏览器

python 复制代码
broswer = webdriver.Chrome()
broswer.quit()   #关闭浏览器

优化

(1)handless

selenium默认是有页面的,所以会存在加载大量css和js。针对这一问题,selenium下的handless可以实现无界面访问,提高访问速度和效率。

python 复制代码
# 基本配制
def share_browser():
     options = webdriver.ChromeOptions()  # 创建配置对象
     options.add_argument('--headless')  # 无头参数,浏览器隐藏在后台运行
     options.add_argument('--window-size=1920x1080')  # 设置浏览器分辨率(窗口大小)
     options.add_argument('--disable-gpu')  # 禁用GPU加速
     browser = webdriver.Chrome(options=options)  # 创建浏览器对象
     return browser

以访问csdn为例演示一个小案例:

python 复制代码
from selenium import webdriver

def share_browser():
     options = webdriver.ChromeOptions()  # 创建配置对象
     options.add_argument('--headless')  # 无头参数,浏览器隐藏在后台运行
     options.add_argument('--window-size=1920x1080')  # 设置浏览器分辨率(窗口大小)
     options.add_argument('--disable-gpu')  # 禁用GPU加速
     browser = webdriver.Chrome(options=options)  # 创建浏览器对象
     return browser

broswer = share_browser()

url = 'https://www.csdn.net/'    # 访问csdn页面

broswer.get(url)
broswer.save_screenshot('csdn.png')

快照

关于options的更多配置,请看:Selenium自动化教程02:浏览器options配置及常用的操作方法_selenium options-CSDN博客

(2) phantomjs

phantomjs也可以实现无界面,但现已经被启用,且操作较handless为麻烦,固不推荐。

相关推荐
一个闪现必杀技7 分钟前
Python入门--函数
开发语言·python·青少年编程·pycharm
小鹿( ﹡ˆoˆ﹡ )27 分钟前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
卷心菜小温42 分钟前
【BUG】P-tuningv2微调ChatGLM2-6B时所踩的坑
python·深度学习·语言模型·nlp·bug
陈苏同学1 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
唐家小妹1 小时前
介绍一款开源的 Modern GUI PySide6 / PyQt6的使用
python·pyqt
Ambition_LAO1 小时前
解决:进入 WSL(Windows Subsystem for Linux)以及将 PyCharm 2024 连接到 WSL
linux·pycharm
羊小猪~~2 小时前
深度学习项目----用LSTM模型预测股价(包含LSTM网络简介,代码数据均可下载)
pytorch·python·rnn·深度学习·机器学习·数据分析·lstm
Marst Code2 小时前
(Django)初步使用
后端·python·django
985小水博一枚呀2 小时前
【对于Python爬虫的理解】数据挖掘、信息聚合、价格监控、新闻爬取等,附代码。
爬虫·python·深度学习·数据挖掘
立秋67892 小时前
Python的defaultdict详解
服务器·windows·python