爬虫视频学习之selenium

Selenium是一个用于web应用程序测试的工具,它可以在真实浏览器中模拟用户操作。通过安装和配置ChromeDriver,可以进行网页元素定位、交互以及无界面测试。文章详细介绍了Selenium的使用步骤,包括元素定位方法如id、xpath和css选择器,以及交互操作如点击、输入、页面导航等。
摘要由CSDN通过智能技术生成

目录

1.什么是selenium?

2.为什么使用selenium?

3.如何安装selenium?

4.selenium使用步骤

5.selenium元素定位(常用)

 6.selenium访问元素信息

7.交互

8.无界面浏览器Chrome handless


1.什么是selenium?
  • selenium是一个用于web应用程序测试的工具
  • selenium测试直接运行在浏览器中,就像真正的用户操作一样
  • 支持通过各种driver,驱动真实浏览器完成测试
  • 支持无界面浏览器操作
2.为什么使用selenium?

模拟浏览器功能,自动执行网页中的js代码,实现动态加载

3.如何安装selenium?
  1. 操作谷歌浏览器驱动下载地址
  2. 谷歌驱动和谷歌浏览器版本之间的映射表
  3. 查看谷歌浏览器版本
  4. pip install selenium
4.selenium使用步骤
#(1)导入selenium
from selenium import webdriver
#(2)创建浏览器操作对象
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
#(3)访问网站
url = 'https://www.baidu.com'
browser.get(url)
5.selenium元素定位(常用)

from selenium import webdriver

path = 'chromedriver.exe'

browser=webdriver.Chrome(path)

url='https://www.baidu..com'

browser.get(url)

(1)#根据id找对象

button=browser.find_element_by_id('su')

print(button)

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

button=browser.find_elements_by_xpath('//input[@id="su"]')

print(button)

(3)  #根据bs4的语法获取对象

button=browser.find_element_by_css_selector('#su')

print(button)

 6.selenium访问元素信息

获取元素属性[ .get_attribute('class') ]

获取元素文本[ .text ]

获取标签名[ .tag_name ]

7.交互

点击:click()

输入:send_keys()

后退操作:browser.back()

前进操作:browser.forward()

模拟JS滚动:

js_button='document.documentElement.scrollTop=100000'
browser.execute_script(js)  执行JS代码

获取网页代码:page_source

from selenium import webdriver
#创建浏览器对象
path='chromedriver.exe'
browser=webdriver.Chrome(path)
#url
url='https://www.baidu.com'
browser.get(url)
import time
time.sleep(2)
#获取文本框的对象
input=browser.find_element_by_id('kw')
#在文本框中输入周杰伦
input.send_keys("周杰伦")
time.sleep(2)
#获取“百度一下”的按钮
button=browser.find_element_by_id('su')
#点击按钮
button.click()
time.sleep(2)
#滑到底部
js_button='document.documentElement.scrollTop=100000'
browser.execute_script(js_bottom)
time.sleep(2)
#获取下一页的按钮
next=browser.find_element_by_xpath('//a[@class="n"]')
#点击下一页
next.click()
time.sleep(2)
#回到上一页
browser.back()
time.sleep(2)
#回去
browser.forward()
time.sleep(3)
#退出
browser.quit()
8.无界面浏览器Chrome handless

Chrome handless 模式,是谷歌针对Chrome浏览器59版新增的一种模式,可以让你不打开UI界面的情况下使用Chrome浏览器,运行效果与Chrome保持一致,并且性能更高

配置:

可以看做封装的handless

from selenium import webdriver
from selenium.webdriver.chrome.options import Options 


chrome_options=Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')


path=r'C:\Program File(x86)\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location=path


browser=webdriver.Chrome(chrome_options=chrome_options)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options 

def share_browser():
    chrome_options=Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')


    path=r'C:\Program File(x86)\Google\Chrome\Application\chrome.exe'
    chrome_options.binary_location=path


    browser=webdriver.Chrome(chrome_options=chrome_options)
    return browser
#直接调用share_browser()
browser=share_browser()
url='https://www.baidu.com
browser.get(url)

browser.get('https://www.baidu.com/')

from selenium import webdriver
from selenium.webdriver.chrome.options import Options 
chrome_options=Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#path是你自己的Chrome浏览器的文件路径(唯一更改的部分)
path=r'C:\Program File(x86)\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location=path
browser=webdriver.Chrome(chrome_options=chrome_options)

url='https://www.baidu.com'
browser.get(url)

browser.save_screenshot('baidu.png')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值