Selenium模拟浏览器访问

目录

一、selenium简介

 1.1、什么是selenium

 1.2、为什么使用selenium

 1.3、安装selenium

  1.3.1、谷歌浏览器驱动下载

  1.3.2、谷歌驱动和谷歌浏览器版本之间的映射表

  1.3.3、查看谷歌浏览器版本

  1.3.4、安装selenium库

二、selenium的使用

 2.1、selenium的使用步骤

 2.2、举例

三、selenium元素操作

 3.1、selenium的元素定位

 3.2、selenium访问元素信息

 3.3、selenium交互

四、无界面浏览器 handless

 4.1、handless基本配置——生成浏览器对象

 4.2、handless 封装


一、selenium简介

 1.1、什么是selenium

(1)selenium是一个用于Web应用程序测试的工具。

(2)selenium测试直接运行在浏览器中,就像真正的用户在操作一样。

(3)支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaDriver,ChromeDriver)驱动真正的浏览器完成测试。

(4)selenium支持无界面浏览器操作。

 1.2、为什么使用selenium

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

 1.3、安装selenium

  1.3.1、谷歌浏览器驱动下载

        1》下载地址:http://chromedriver.storage.googleapis.com/index.html

         2》windows(32/64)均下载win32.zip即可

         3》下载后解压:得到chromedriver.exe

          4》将chromedriver.exe文件放置在python程序中即可

  1.3.2、谷歌驱动和谷歌浏览器版本之间的映射表

selenium之 chromedriver与chrome版本映射表(更新至v2.46)_huilan_same的博客-CSDN博客_chromedriver版本更多关于python selenium的文章,请关注我的专栏:Python Selenium自动化测试详解看到网上基本没有最新的chromedriver与chrome的对应关系表,便兴起整理了一份如下,希望对大家有用: chromedriver版本 支持的Chrome版本 v2.37 v64-66 v2.36 v63-65 v2.3...https://blog.csdn.net/huilan_same/article/details/51896672

  1.3.3、查看谷歌浏览器版本

  1.3.4、安装selenium库

二、selenium的使用

 2.1、selenium的使用步骤

(1)导入:from selenium import webdriver

(2)创建谷歌浏览器操作对象:

        path = 谷歌浏览器驱动文件路径

        browser = webdriver.Chrome(path)

(3)访问网址

        url = 要访问的网址

        browser.get(url)

 2.2、举例

# (1)导入selenium
from selenium import webdriver

# (2)创建浏览器操作对象(导入浏览器驱动)
path = 'chromedriver.exe'

browser = webdriver.Chrome(path)

# (3)访问网站
url = 'https://www.baidu.com/'

urlJd = 'https://www.jd.com/'

# 模拟浏览器访问网址
browser.get(urlJd)

# 获取网页源码
content = browser.page_source
print(content)

 

三、selenium元素操作

 3.1、selenium的元素定位

   元素定位:模拟鼠标和键盘操作元素,实现点击、输入等操作。操作元素之前需要先对其进行定位,webDriver提供了多种元素定位的方法。

# 元素定位(根据标签属性的属性值获取对象)
# find_element  返回对象
# find_elements 返回对象列表

# 根据id返回对象
button1 = browser.find_element(by=By.ID, value='su')

# 根据标签属性的属性值返回对象
button2 = browser.find_element(by=By.NAME, value='wd')

# 根据xpath返回对象
button3 = browser.find_element(by=By.XPATH, value='//input[@id="su"]')

# 根据标签的名字获取对象
button4 = browser.find_elements(by=By.TAG_NAME, value='input')

# 根据bs4的语法,获取对象
button5 = browser.find_element(by=By.CSS_SELECTOR, value='#su')

# 获取当前页面中的链接文本
button6 = browser.find_element(by=By.LINK_TEXT, value='地图')

 3.2、selenium访问元素信息

        1)获取标签属性值

         2)获取标签名

         3)获取元素文本

from selenium import webdriver
from selenium.webdriver.common.by import By

path = 'chromedriver.exe'

browser = webdriver.Chrome(path)

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

browser.get(url)

input = browser.find_element(by=By.ID, value='su')

# 1、获取标签的属性值
print(input.get_attribute('class'))

# 2、获取标签名
print(input.tag_name)

# 3、获取元素文本
a = browser.find_element(by=By.LINK_TEXT, value='新闻')
print(a.text)

 3.3、selenium交互

        

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 创建浏览器对象
path = 'chromedriver.exe'

browser = webdriver.Chrome(path)

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

browser.get(url)

time.sleep(2)

# 获取文本框的对象
input = browser.find_element(by=By.ID, value='kw')

# 在文本框中输入内容
input.send_keys('周杰伦')
time.sleep(2)

# 获取百度一下的按钮
button = browser.find_element(by=By.ID, value='su')

# 点击按钮
button.click()
time.sleep(2)

# 划到页面底部
js_bottom = 'document.documentElement.scrollTop=100000'
browser.execute_script(js_bottom)
time.sleep(2)

# 获取下一页的按钮
next = browser.find_element(by=By.XPATH, value='//a[@class="n"]')

# 点击下一页
next.click()
time.sleep(2)

# 回到上一页
browser.back()
time.sleep(2)

# 回去
browser.forward()
time.sleep(3)

# 退出
browser.quit()

四、无界面浏览器 handless

        Chrome-handless 模式,Google针对Chrome浏览器 59版 新增加一种模式,可以在不打开UI界面的情况下使用 Chrome 浏览器,但是其运行效果与 Chrome 保持一致。

 4.1、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是chrome浏览器的文件路径
path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path

browser = webdriver.Chrome(chrome_options=chrome_options)

 4.2、handless 封装

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是chrome浏览器的文件路径
    path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
    chrome_options.binary_location = path

    browser = webdriver.Chrome(chrome_options=chrome_options)
    return browser

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


版权声明:本文为weixin_44302046原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。