浏览器自动化测试工具Selenium的安装与试用

配置及安装

建立目录,打开cmd,创建python虚拟环境,然后通过pip安装Selenium即可。

:: 创建虚拟环境
D:\PATH\selenium-tools>python -m venv venv
:: 更新pip工具
D:\PATH\selenium-tools>venv\Scripts\python.exe -m pip install --upgrade pip
:: 安装Selenium
D:\PATH\selenium-tools>venv\Scripts\python.exe -m pip install selenium

在目录下创建一个bat脚本apply_env.bat,方便应用虚拟环境和运行之后的任务脚本

@Echo off
Title Selenium Env                                        __Create by Chris@2023/09/01

if not defined VENV_DIR (set "VENV_DIR=%~dp0%venv")
set PYTHON="%VENV_DIR%\Scripts\Python.exe"

doskey ls=dir /B $*
doskey py=%PYTHON% $*

call %VENV_DIR%\Scripts\activate.bat
cmd /k

将之前下载的驱动应用chromedriver.exe拷贝到同目录下,这样在不改变环境变量下即可直接调用执行。

简单使用

在百度中搜索“xvcat.com”:

from time import sleep
from selenium import webdriver

# 使用Chrome驱动器,全局设置隐式等待,调整窗口大小
driver = webdriver.Chrome()
driver.implicitly_wait(2)
driver.set_window_size(1200, 675)

# 打开指定路径的页面
driver.get('https://www.baidu.com/')

# 定位搜索框,并输入内容
driver.find_element("xpath", "//input[@id='kw']").send_keys("xvcat.com")
# 点击搜索
driver.find_element("xpath", "//input[@type='submit']").click()

sleep(1)
print('load finish')

打开入口脚本,运行这个任务:

《浏览器自动化测试工具Selenium的安装与试用》有2条评论

  1. My spouse and I stumbled over here from a different web
    address and thought I might check things out. I like what I see so now
    i am following you. Look forward to looking at your web page yet again.

    回复
  2. I was very pleased to discover this web site. I need to to thank you for your time due to this fantastic read!!
    I definitely loved every bit of it and I have you bookmarked to check out new stuff in your site.

    回复

发表评论

error: Content is protected !!