【Python爬虫01】Selenium简单认识


在现代的Web开发中,自动化测试是不可或缺的一环。而Selenium是一个强大的Python库,用于自动化Web浏览器的操作和测试。本篇博文将为您介绍Selenium的基础知识和使用方法,并通过案例说明如何进行Web自动化测试。

什么是Selenium?

Selenium是一个开源的自动化测试框架,主要用于模拟用户与Web浏览器的交互。它可以在不同的浏览器中执行操作,如点击按钮、填写表单、提取数据等,从而实现自动化的Web测试。

Selenium支持多种编程语言,其中Python是最常用的一种。Python提供了丰富的库和工具,使得使用Selenium进行Web自动化测试变得更加简单和高效。
在这里插入图片描述

安装Selenium

在开始之前,我们需要安装Selenium库。可以通过pip命令来进行安装:

pip install selenium

此外,还需要下载相应的浏览器驱动程序,例如ChromeDriver或GeckoDriver,以便Selenium可以与浏览器进行交互。根据使用的浏览器类型和版本,下载对应的驱动程序,并将其添加到系统路径中。

chromedriver下载与安装方法

使用Selenium进行Web自动化测试

1. 导入必要的库

首先,我们需要导入Selenium库以及其他必要的库:

from selenium import webdriver

2. 创建浏览器驱动对象

接下来,我们需要创建一个浏览器驱动对象,用于控制和操作浏览器。以Chrome浏览器为例:

driver = webdriver.Chrome()

3. 打开网页

使用驱动对象打开所需的网页:

driver.get("https://www.example.com")

4. 查找元素和操作

可以使用不同的方法来查找网页上的元素,并对其进行操作。例如,通过ID、类名或XPath等方式来定位元素:

element = driver.find_element(By.ID,"element_id")
element.click()

5. 填写表单和提交

对于需要填写表单的情况,可以使用send_keys()方法来输入文本,并使用submit()方法提交表单:

input_field = driver.find_element(By.ID,"input_field_id")
input_field.send_keys("Hello, World!")
input_field.submit()

6. 关闭浏览器

完成测试后,记得关闭浏览器:

driver.quit()

以上是一个简单的Selenium测试的基本流程。通过查找元素、操作表单和提交等步骤,您可以实现各种自动化的Web测试。

示例:自动登录网站

让我们通过一个案

例来演示如何使用Selenium进行自动登录网站的操作。

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# 创建浏览器驱动对象
driver = webdriver.Chrome()
driver.maximize_window() # 窗口最大化

# 打开登录页面
driver.get("https://portrait.gitee.com/login")

# 填写用户名和密码
try:
    username_field = driver.find_element(By.NAME, 'user[login]')
    username_field.send_keys("这里输入你的账号")
    password_field = driver.find_element(By.NAME, 'user[password]')
    password_field.send_keys("这里输入你的密码")

    remember_me_field = driver.find_element(By.XPATH, '//*[@id="new_user"]/div/div/div/div[3]/div[1]/div/label')
    remember_me_field.click()
except Exception as e:
    print(f"[+] error:{e[:20]}")
    
time.sleep(2)
# 登录
commit_field = driver.find_element(By.NAME, 'commit').click()

time.sleep(10)
# 执行其他操作,如点击链接、提取数据等

# 关闭浏览器
driver.quit()

通过上述代码,您可以模拟用户登录网站的操作,输入用户名和密码,并点击登录。

演示视频:

gitee登录自动登录演示

总结:
本篇博文介绍了使用Selenium进行Web自动化测试的基础知识和使用方法。您学习了如何安装Selenium库、创建浏览器驱动对象以及执行各种操作来模拟用户行为。通过具体案例的演示,您可以更好地理解和应用Selenium在Web自动化测试中的作用。希望本篇博文能够帮助您入门Selenium,并提升您的自动化测试技能。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
v2.45.0 ======= * Supports native events for Firefox versions 31 (current ESR), and 24 (immediately previous ESR). Native event support has been discontinued for versions of Firefox later than 33. * Removed automatic installation of SafariDriver extention for .NET. From this point forward, users are expected to manually install the SafariDriver extension into their Safari installation in order to drive the browser. This is due to Apple's changes in architecture for Safari extensions. * Added initial implementation of .NET bindings OperaDriver. The .NET bindings will now support the Chromium-based Opera driver without requiring the use of the Java remote WebDriver server. This driver will work with Opera 26 and above, and requires the download of the Opera driver executable. Code cleanup and refactoring will take place under a separate commit. Note that there is still no support in the .NET bindings for the Presto-based Opera without using the remote server, nor is there likely to be. * Added option to not delete anonymous Firefox profile in .NET. This change adds an option to the .NET FirefoxProfile class so that the driver will not delete the anonymous profile created by the driver. Since the driver cannot and should not use an existing profile in situ because of the multiple instance automation case, this change means that modifications applied to the anonymous profile can be retained and used in future anonymous profiles. The implication is that the user can now make modifications to a profile, and retain those profile modifications (e.g., cookies) into other future profiles, simulating persistent changes over multiple browser launches. Fixes issue #7374. * Introduced type safe option in InternetExplorerOptions to set the capability to disable check of mime type of the doucment when setting cookies. When setting cookies, there is a check in the IE driver to validate that the page in the browser is, in fact, an HTML page. Despite the fact that omitting this check can cause unrecoverable crashes in the driver, there is demand for a mechanism to disable this check for older, legacy versions of Internet Explorer. Fixes issue #1227. v2.44.0 ======= * Supports native events for Firefox versions 33 (current), 32 (immediately previous release), 31 (current ESR), and 24 (immediately previous ESR). * Rolled back improper spec compliance for finding elements. * Fixed WebDriverBackedSelenium compatibility with IE5. Fixes issue #7938. v2.43.1 ======= * Point-release to correct version resources in .NET bindings assemblies. No functional changes. v2.43.0 ======= * Supports native events for Firefox versions 32 (current), 31 (immediately previous release and current ESR), and 24 (immediately previous ESR). * Integrated the Microsoft Internet Explorer driver implementation into the .NET bindings. By setting the Implementation property of the .NET bindings' InternetExplorerDriverService class, the user can now force the driver to use the Microsoft implementation. Note that the default is to use the existing open-source implementation based on the Automation Atoms. Over time as the Microsoft implementation matures, this will be switched to use the Microsoft implementation, first by default, then exclusively. To use the Microsoft implementation, the user must have the August 2014 updates to Internet Explorer installed through Windows Update, and must install the IE Web Driver Tool for Internet Explorer 11 download from Microsoft (http://www.microsoft.com/en-us/download/details.aspx?id=44069). * Added safe check for window.localStorage in .NET WebDriverBackedSelenium implementation. Patch provided by Timofey Vasenin. * Implemented pluggable element locator factories for .NET PageFactory. This change allows the user to specify a custom IElementLocatorFactory for locating elements when used with the PageFactory. This gives much more control over the algorithm used to locate elements, and allows the incorporation of things like retries or handling of specific exceptions. * Issue #7367: Set Json.NET to ignore dates when parsing response values. * Issue #7419: Added support for SwitchTo().ParentFrame() in .NET bindings. This brings the .NET bindings into parity with other languages. * Belatedly removed long-obsolete .NET tests for AndroidDriver.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸峰轻云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值