selenium IDE-自动写selenium代码
学习完selenium后有一个问题,如果web操作过程非常多,比如有十几二十几个操作,就会导致要写非常多的代码,下面的神器可以自动依据你操作网页的步骤生成相应的代码,
它就是selenium IDE
首先需要安装谷歌插件

进入插件首页

点击 record a new test in a new project(在一个新的项目中记录一个新的案例)

输入你的项目名字,好记都行

输入你的基础网页,比如https://www.baidu.com
点击 start record (开始记录)
于是会跳转到该页面

进行你的网页操作,进行完后



查看代码
# 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 Test1():
def setup_method(self, method):
self.driver = webdriver.Chrome()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def wait_for_window(self, timeout = 2):
time.sleep(round(timeout / 1000))
wh_now = self.driver.window_handles
wh_then = self.vars["window_handles"]
if len(wh_now) > len(wh_then):
return set(wh_now).difference(set(wh_then)).pop()
def test_1(self):
self.driver.get("https://www.baidu.com/")
self.driver.set_window_size(1046, 760)
self.vars["window_handles"] = self.driver.window_handles
self.driver.find_element(By.CSS_SELECTOR, ".hotsearch-item:nth-child(1) .title-content-title").click()
self.vars["win2252"] = self.wait_for_window(2000)
self.driver.switch_to.window(self.vars["win2252"])
element = self.driver.find_element(By.LINK_TEXT, "甘肃地震救援工作已基本结束")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
element = self.driver.find_element(By.CSS_SELECTOR, "body")
actions = ActionChains(self.driver)
actions.move_to_element(element, 0, 0).perform()
self.driver.find_element(By.ID, "content_left").click()
self.vars["window_handles"] = self.driver.window_handles
self.driver.find_element(By.XPATH, "//div[@id=\'4\']/div/div/h3/a/em").click()
self.vars["win6291"] = self.wait_for_window(2000)
self.driver.switch_to.window(self.vars["win6291"])

浙公网安备 33010602011771号