编写第一个Selenium脚本

目录

安装Selenium类库

请求对应的程序语言

Pip

下载

在项目中使用

编写第一个Selenium脚本

八个基本组成部分

1. 使用驱动实例开启会话

本地驱动

驱动自动管理

驱动选项

浏览器选项

Capabilities

Timeouts

2. 在浏览器上执行操作

3. 请求 浏览器信息

4. 建立等待策略

5. 发送命令 查找元素

6. 操作元素

7. 获取元素信息

8. 结束会话


安装Selenium类库

配置自动化的浏览器.

首先,您需要为自动化项目安装 Selenium 绑定库。 库的安装过程取决于您选择使用的语言。

请求对应的程序语言

该库所支持的Python版本最低版本可以在 支持的Python版本 章节中找到 PyPi

这里提供了几种不同的方式来安装 Selenium .

Pip

pip install selenium

下载

此外你可以从这里下载 PyPI source archive (selenium-x.x.x.tar.gz) 并通过: setup.py 文件安装:

python setup.py install

在项目中使用

为了在项目中使用它,需要将它添加到 requirements.txt 文件中:

selenium==4.16.0

逐步构建一个Selenium脚本的说明

当你完成 Selenium安装 后, 便可以开始书写Selenium脚本了.

编写第一个Selenium脚本

八个基本组成部分

Selenium所做的一切, 就是发送给浏览器命令, 用以执行某些操作或为信息发送请求. 您将使用Selenium执行的大部分操作, 都是以下基本命令的组合

1. 使用驱动实例开启会话

driver = webdriver.Chrome()

关于如何启动会话,请浏览文档 驱动会话,驱动会员用于启动和停止会话, 用于打开和关闭浏览器.

会话是通过初始化新的驱动类对象自动创建的.每种语言都允许使用来自这些类 (或等效类) 之一的参数创建会话,可以创建本地驱动,这个需要下载驱动程序,或者使用webdriver-manager 库来自动管理驱动安装,以下分别是本地方式指定驱动位置和webdriver-manager管理驱动(推荐)

本地驱动

启动本地驱动的首要唯一参数 包括在本地计算机上有关启动所需驱动服务的信息

 self.driver = webdriver.Chrome(executable_path='F:\\PycharmProjects\\PythonPractise\\selenium_po\\driver\\chromedriver.exe')
驱动自动管理

 webdriver-manager可以根据用户环境的浏览器版本自行匹配最适合的驱动程序。

安装管理器:

pip install webdriver-manager

以 Chrome 为例

# selenium 3 
from  selenium  import  webdriver 
from  webdriver_manager.chrome  import  ChromeDriverManager 
 
driver  =  webdriver . Chrome ( ChromeDriverManager () .install())
# selenium 4 
from  selenium  import  webdriver 
from  selenium.webdriver.chrome.service  import  Service  as  ChromeService 
from  webdriver_manager.chrome  import  ChromeDriverManager 
 
driver  =  webdriver . Chrome (service= ChromeService ( ChromeDriverManager () . install ()))
驱动选项

选项 描述您想要的会话类型; 默认值为local,但是对于remote则是必须设置的。

浏览器选项

这些capabilities用于所有浏览器.在 Selenium 3 中, capabilities是借助"Desired Capabilities"类定义于会话中的. 从 Selenium 4 开始, 您必须使用浏览器选项类. 对于远程驱动程序会话, 浏览器选项实例是必需的, 因为它确定将使用哪个浏览器.这些选项在 Capabilities 的 w3c 规范中进行了描述.

每个浏览器都有 自定义选项 , 是规范定义之外的内容.

例如:

Capabilities
Capability Key Value Type Description
Browser name "browserName" string Identifies the user agent.
Browser version "browserVersion" string Identifies the version of the user agent.
Platform name "platformName" string Identifies the operating system of the endpoint node.
Accept insecure TLS certificates "acceptInsecureCerts" boolean Indicates whether untrusted and self-signed TLS certificates are implicitly trusted on navigation for the duration of the session.
Page load strategy "pageLoadStrategy" string Defines the current session’s page loading strategy.
Proxy configuration "proxy" JSON Object Defines the current session’s proxy configuration.
Window dimensioning/positioning "setWindowRect" boolean Indicates whether the remote end supports all of the resizing and repositioning commands.
Session timeouts "timeouts" JSON Object Describes the timeouts imposed on certain session operations.
Strict file interactability "strictFileInteractability" boolean Defines the current session’s strict file interactability.
Unhandled prompt behavior "unhandledPromptBehavior" string Describes the current session’s user prompt handler. Defaults to the dismiss and notify state.
Timeouts
Field Default JSON key Optional† Nullable Description†
Script timeout 30,000 "script"

Specifies when to interrupt a script that is being evaluated.

A null value implies that scripts should never be interrupted, but instead run indefinitely.

Page load timeout 300,000 "pageLoad"

Provides the timeout limit used to interrupt an explicit navigation attempt.

Implicit wait timeout 0 "implicit"

Specifies a time to wait for the element location strategy to complete when locating an element

2. 在浏览器上执行操作

在本例中, 我们 导航 到一个网页.

driver.get("https://www.selenium.dev/selenium/web/web-form.html")

3. 请求 浏览器信息

您可以请求一系列关于浏览器的信息 , 包括窗口句柄、浏览器尺寸/位置、cookie、警报等.

title = driver.title

4. 建立等待策略

将代码与浏览器的当前状态同步 是Selenium面临的最大挑战之一, 做好它是一个高级主题.

基本上, 您希望在尝试定位元素之前, 确保该元素位于页面上, 并且在尝试与该元素交互之前, 该元素处于可交互状态.

隐式等待很少是最好的解决方案, 但在这里最容易演示, 所以我们将使用它作为占位符.

阅读更多关于等待策略 的信息.

driver.implicitly_wait(0.5)

5. 发送命令 查找元素

大多数Selenium会话中的主要命令都与元素相关, 如果不先找到元素, 就无法与之交互.

6. 操作元素

对于一个元素, 只有少数几个操作可以执行, 但您将经常使用它们.

text_box.send_keys("Selenium")
submit_button.click()

7. 获取元素信息

元素存储了很多被请求的信息.

text = message.text

8. 结束会话

这将结束驱动程序进程, 默认情况下, 该进程也会关闭浏览器. 无法向此驱动程序实例发送更多命令.

See Quitting Sessions.

driver.quit()

完整代码:

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

driver = webdriver.Chrome()

driver.get("https://www.selenium.dev/selenium/web/web-form.html")

title = driver.title

driver.implicitly_wait(0.5)

text_box = driver.find_element(by=By.NAME, value="my-text")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")

text_box.send_keys("Selenium")
submit_button.click()

message = driver.find_element(by=By.ID, value="message")
text = message.text

driver.quit()

猜你喜欢

转载自blog.csdn.net/TalorSwfit20111208/article/details/135029289