【Selenium】导出成py脚本的基础使用

0. Selenium简介

Selenium是一个用于Web应用程序测试的工具。

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

主流浏览器都支持。例如firefox,在插件市场里面就可以直接下载selenium IDE。

image-20210430115928011

image-20210430120023959

1. 脚本录制

image-20210430120120321

image-20210430120253884

2

2. 导出成Py脚本

image-20210430120511933

image-20210430120524959

3. 运行脚本

导出脚本成功,桌面就出现了一个.py文件。但是这时候是没办法直接运行的。

因为还缺少,运行脚本所需要的驱动。

https://github.com/mozilla/geckodriver/releases

得去github下载。

image-20210430120842550

下载好后,把他们放在一个文件夹里,使用IDE打开。

image-20210430120929122

此时你发现,test_untitled.py 直接运行是没有效果的。

image-20210430121059635

同时还得配置driver。

重点圈图:

image-20210430121914577

image-20210430121935551

完整实例代码:
# 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 DesiredCapabilities


class TestUntitled():
    def setup_method(self, method=None):
        # ! 这边的method请将默认值设置为None
        self.driver = webdriver.Firefox(
            executable_path=r'D:\Personal\Desktop\tmp\geckodriver-v0.29.1-win64\geckodriver.exe')
        # ! 请一定要改成自己的driver地址 (请使用绝对路径,比较稳妥)
        self.vars = {}

    def teardown_method(self, method):
        self.driver.quit()

    def test_untitled(self):
        self.driver.get("https://www.baidu.com/")
        self.driver.set_window_size(993, 696)
        self.driver.find_element(By.ID, "kw").click()
        element = self.driver.find_element(By.CSS_SELECTOR, "area")
        actions = ActionChains(self.driver)
        actions.move_to_element(element).perform()
        self.driver.find_element(By.ID, "kw").send_keys("11111")


if __name__ == '__main__':
    # ! main方法中进行调用
    test = TestUntitled()
    test.setup_method()
    test.test_untitled()
    time.sleep(2)
    # 关闭程序
    test.teardown_method()
运行演示:

3

  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夏2同学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值