日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/1/21 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Selenium】导出成py脚本的基础使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

0. Selenium簡介

Selenium是一個用于Web應用程序測試的工具。

Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。

主流瀏覽器都支持。例如firefox,在插件市場里面就可以直接下載selenium IDE。

1. 腳本錄制

2. 導出成Py腳本

3. 運行腳本

導出腳本成功,桌面就出現了一個.py文件。但是這時候是沒辦法直接運行的。

因為還缺少,運行腳本所需要的驅動。

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

得去github下載。

下載好后,把他們放在一個文件夾里,使用IDE打開。

此時你發現,test_untitled.py 直接運行是沒有效果的。

同時還得配置driver。

重點圈圖:

完整實例代碼:

# 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 TestUntitled():def setup_method(self, method=None):# ! 這邊的method請將默認值設置為Noneself.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()

運行演示:

總結

以上是生活随笔為你收集整理的【Selenium】导出成py脚本的基础使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。