当前位置: 首页 > news >正文

python安装selenium,geckodriver,chromedriver,Selenium IDE

安装浏览器

找到浏览器的版本号

chrome 版本 130.0.6723.92(正式版本) (64 位)

firfox 116.0.3 (64 位),但是后面运行的时候又自动更新到了 127.0.0.8923

安装selenium

> pip install selenium
> pip show selenium
Name: selenium
Version: 4.26.1
Summary: Official Python bindings for Selenium WebDriver
Home-page: https://www.selenium.dev
Author:
Author-email:
License: Apache 2.0
Location: d:\programdata\anaconda3\lib\site-packages
Requires: certifi, trio, trio-websocket, typing_extensions, urllib3, websocket-client
Required-by:

其原理是 selenium 通过浏览器驱动向浏览器发送指令。selenium 目的在于web testing。

selenium 文档

https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

https://github.com/SergeyPirogov/webdriver_manager

安装浏览器驱动的方式有两种

一、手动下载
浏览器支持的操作系统维护者下载问题追溯
Chromium/ChromeWindows/macOS/LinuxGoogle下载Issues
FirefoxWindows/macOS/LinuxMozilla下载Issues
EdgeWindows/macOS/LinuxMicrosoft下载Issues
Internet ExplorerWindowsSelenium Project下载Issues
SafarimacOS High Sierra and newerApple内置Issues

备注:Opera驱动不再适用于Selenium的最新功能,目前官方不支持。

将下载的压缩包解压,然后添加到环境变量, 确保可以直接使用命令访问到

firefox驱动下载

firefox的驱动是geckodriver,仓库地址为:https://github.com/mozilla/geckodriver

下载 https://github.com/mozilla/geckodriver/releases/download/v0.34.0/geckodriver-v0.34.0-win64.zip

geckodriver.exe --versiongeckodriver 0.34.0 (c44f0d09630a 2024-01-02 15:36 +0000)The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
my_browser = webdriver.Firefox()
try:my_browser.get("https://www.baidu.com")input = my_browser.find_element(By.ID, "kw") # html代码中输入框的id是kw 其他界面相应的找到idinput.send_keys("hello world")  # 向输入框内部输入hello worldinput.send_keys(Keys.ENTER)  # 输入完之后回车time.sleep(10)  # 等待页面加载完毕
finally:my_browser.close()  # 关闭浏览器
Problem reading geckodriver versions: error sending request for url (https://raw.githubusercontent.com/SeleniumHQ/selenium/trunk/common/geckodriver/geckodriver-support.json). Using latest geckodriver version
The geckodriver version (0.34.0) detected in PATH at D:\geckodriver-v0.34.0-win64\geckodriver.exe might not be compatible with the detected firefox version (127.0.0.8923
); currently, geckodriver 0.35.0 is recommended for firefox 127.*, so it is advised to delete the driver in PATH and retry

但是 firefox的确是启动了。

但是从错误信息中我们发现了重要信息,把Json文件下载下来,可以看到里面列出了 geckodriver 与 firefox 版本的对应,比如

{"geckodriver-releases": [{"geckodriver-version": "0.35.0","min-firefox-version": 115},{"geckodriver-version": "0.34.0","min-firefox-version": 115},{"geckodriver-version": "0.33.0","min-firefox-version": 102,"max-firefox-version": 120},......

的确,firefox-127.x 需要使用 geckodriver-0.35.0,这也是错误信息中提到的。但是在运行的时候是需要开梯子的,不然它拉取不到这个json文件。于是我们下载 geckodriver-0.35.0-win64,解压之后替换原来的。

geckodriver.exe --versiongeckodriver 0.35.0 (9f0a0036bea4 2024-08-03 07:11 +0000)The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.

再次运行代码,没有任何报错,且浏览器行为正常。

在这里插入图片描述

chrome驱动下载

1、https://registry.npmmirror.com/binary.html?path=chromedriver

2、https://chromedriver.chromium.org/downloads

3、https://developer.chrome.com/docs/chromedriver/downloads?hl=zh-cn

文档中给的下载链接(2和3)没有找到下载按钮,而且最高只有114开头的版本,于是我使用 链接1 下载了一个,版本号为 114.0.5735.90,还是win32的,由于它没有提供更高的版本,我打算运行试一下。

chromedriver.exe --versionChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052})
from selenium import webdriver
driver = webdriver.Chrome()

报错了,但是从报错的信息中找到了有用信息

Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)
The chromedriver version (114.0.5735.90) detected in PATH at D:\geckodriver-v0.34.0-win64\chromedriver.exe might not be compatible with the detected chrome version (130.
0.6723.92); currently, chromedriver  is recommended for chrome 130.*, so it is advised to delete the driver in PATH and retry
Error sending stats to Plausible: error sending request for url (https://plausible.io/api/event)

把这个json文件下载下来,里面包含了所有版本的chrone对应的chromedriver版本以及下载地址。没有找到 130.0.6723.92,于是选择了 https://storage.googleapis.com/chrome-for-testing-public/130.0.6723.91/win64/chromedriver-win64.zip

解压之后替换掉原来的

chromedriver.exe --versionChromeDriver 130.0.6723.91 (53ac076783696778ecc8f360ea31765c29c240ad-refs/branch-heads/6723@{#1517})

将上面代码中的 Firefox改成 Chrome 即可,运行

在这里插入图片描述

二、使用 Selenium Manager (未测试)

Before Selenium managed drivers itself, other projects were created to do so for you.

If you can’t use Selenium Manager because you are using an older version of Selenium (please upgrade), or need an advanced feature not yet implemented by Selenium Manager, you might try one of these tools to keep your drivers automatically updated:

  • WebDriverManager (Java)
  • WebDriver Manager (Python)
  • WebDriver Manager Package (.NET)
  • webdrivers gem (Ruby)
pip install webdriver-manager
# selenium 4
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManagerdriver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
三、Selenium IDE

在firefox浏览器上安装 Selenium IDE 扩展,在 Chrome 上也有,你可以在浏览器上创建和进行 selenium test,这比你写代码来调试方便的多,最后再整合到代码里去自动化运行。

Selenium IDE 文档

https://www.selenium.dev/selenium-ide/

https://www.selenium.dev/selenium-ide/docs/en/introduction/getting-started

以firefox为例

点击扩展中的 Selenium IDE,启动此工具
在这里插入图片描述
new 一个项目

名称:baidu

URL:http://www.baidu.com

于是就弹出了一个新窗口,打开了URL的页面,接这你在这个URL里面的所有操作都被记录了下来。
在这里插入图片描述
比如里面的id=kw,它其实是分析了页面,因为这个输入框的id就是kw。

可以点击那个红色按钮Stop recording,然后点击Save project,比如我保存到 D:/selenium-test,你可以点击回放这个project。

有了这个project之后,一个很重要的东西就是 Command-line Runner

You can now run all of your Selenium IDE tests on any browser, in parallel, and on a Grid without needing to write any code.

Code Export

You can export either a test or suite of tests to WebDriver code by right-clicking on a test or a suite, selecting Export, choosing your target language, and clicking Export.
在这里插入图片描述
在这里插入图片描述
The exported code for Python pytest is built to work with Python 3, pytest 4.6.x, and the latest version of Selenium.

You should be able to take the exported JavaScript file and run it after installing these dependencies (e.g., with pip3 install).

Here’s a sample requirements.txt to help you get started.

pip install pytest, selenium

导出的代码如下

# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesclass TestSearch():def setup_method(self, method):self.driver = webdriver.Firefox()self.vars = {}def teardown_method(self, method):self.driver.quit()def test_search(self):self.driver.get("https://www.baidu.com/")self.driver.set_window_size(550, 692)self.driver.find_element(By.ID, "kw").click()self.driver.find_element(By.ID, "kw").send_keys("hello world")self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)time.sleep(1000)
pytest -s test/test_search.py

需要打开梯子


http://www.mrgr.cn/news/66839.html

相关文章:

  • 超萌!HTMLCSS:超萌卡通熊猫头
  • Django学习-项目部署
  • 什么是安全威胁情报?
  • SL6605 输入0.8-5.5V 单颗锂电池驱动LED升压恒流限流方案
  • 如何设置定时关闭或启动整个docker而不是某个容器
  • 入门必读:深度解析如何利用Coze构建企业级知识库的详尽指南
  • ei会议检索!智能控制、测量、信号系统等方向可投!
  • Linux(CentOS)安装 JDK
  • Nvidia突袭AI江湖!悄悄发布新模型,完爆OpenAI和Anthropic?
  • 美国最欢迎这些人!盘点10大移民美国最具优势职业!
  • 【Git】Git常用命令
  • 迅为RK3568开发板支持银河麒麟和开放麒麟系统
  • 【 院士、校长、杰青、Fellow等大咖齐聚!IEEE独立出版】第六届机器人、智能控制与人工智能国际学术会议(RICAI 2024,12月6-8日)
  • DevEco在设备上运行hap报错: Error message: The caller is not a system application
  • 【新手入门软件测试--该如何分辨前后端问题及如何定位日志--前后端问题分辨与日志定位查询问题】
  • ssm063基于SSM框架的德云社票务系统的设计与实现+vue(论文+源码)_kaic
  • wifiTrackerlib之监听wifi相关的Broadcast
  • ENSP RIP动态路由
  • 为什么beyond4二进制比较,字节数相同,但是提示却有差异
  • 前端前置——ajax
  • Java反射API与面向对象编程:当“X光机”遇上“家族相册”
  • C#语言发展历史
  • 华为eNSP:VLAN聚合
  • 纵然千万数据流逝,唯独vector长存
  • JS类型检测
  • 【云原生开发】如何通过client-go来操作K8S集群