前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >web爬虫-Selenium IDE安装使用

web爬虫-Selenium IDE安装使用

作者头像
XXXX-user
修改2019-07-31 10:20:43
1.4K0
修改2019-07-31 10:20:43
举报
文章被收录于专栏:不仅仅是python不仅仅是python

今天将学习使用一个非常有用的浏览器插件Selenium IDE,用于网站的测试和自动化,这里以谷歌浏览器作为测试。

插件下载地址为:

https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd

我们点击添加至chrome完成插件的安装:

插件安装完成后会在导航栏出现一个se的图标:

我们点击图标会弹出一个对话框,让我们选择如何创建项目:

这里我们选择第一项Record a new test in a new project(新建项目并记录一个新的测试),输入项目名称taobao-serach(用淘宝作为测试网址):

点击ok后,输入要监控的网址:

点击START RECORDING开始记录,会自动打开淘宝网页 并出现记录标记:

在弹出的IDE中点击右上方的红色停止记录按钮后中间会出现所有动作的记录过程:

点击上图中左侧区域中的Untitled为这个测试案例命名为hjc element search:

接下来看IDE中基本的操作按钮释义:

点击左侧测试案例可将用例导出:

这里我们导出为python的pytest用例:

会生成一个python文件名字为test_hjchelmentsearch.py:

具体代码如下:

代码语言:javascript
复制
# 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

class TestHjchelmentsearch():
  def setup_method(self, method):
    self.driver = webdriver.Chrome('chromedriver.exe')
    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_hjchelmentsearch(self):
    self.driver.get("https://www.taobao.com/")
    self.driver.set_window_size(1382, 744)
    self.driver.find_element(By.ID, "q").send_keys(Keys.DOWN)
    self.driver.find_element(By.ID, "q").send_keys("hjc头盔")
    self.driver.find_element(By.ID, "q").send_keys(Keys.ENTER)
    self.vars["window_handles"] = self.driver.window_handles
    self.driver.find_element(By.ID, "J_Itemlist_Pic_42600889855").click()
    self.vars["win7263"] = self.wait_for_window(2000)
    self.vars["root"] = self.driver.current_window_handle
    self.driver.switch_to.window(self.vars["win7263"])
    self.driver.execute_script("window.scrollTo(0,78)")
    self.driver.find_element(By.CSS_SELECTOR, "li:nth-child(8) > a > span").click()
    element = self.driver.find_element(By.CSS_SELECTOR, ".tb-img > li:nth-child(6) span")
    actions = ActionChains(driver)
    actions.move_to_element(element).perform()
    element = self.driver.find_element(By.CSS_SELECTOR, "body")
    actions = ActionChains(driver)
    actions.move_to_element(element, 0, 0).perform()
    element = self.driver.find_element(By.CSS_SELECTOR, ".tb-prop:nth-child(1) li:nth-child(6) span")
    actions = ActionChains(driver)
    actions.move_to_element(element).perform()
    self.driver.find_element(By.CSS_SELECTOR, ".tb-prop:nth-child(1) li:nth-child(6) span").click()
    element = self.driver.find_element(By.CSS_SELECTOR, "body")
    actions = ActionChains(driver)
    actions.move_to_element(element, 0, 0).perform()
    self.driver.close()
    self.driver.switch_to.window(self.vars["root"])
    self.driver.close()


#以下是我自己加入的初始化运行代码
hjc = TestHjchelmentsearch()
hjc.setup_method(None)
hjc.test_hjchelmentsearch()
hjc.teardown_method()

如下代码可运行作为测试用例。当我们点击IDE中的运行测试按钮后会自动调用浏览器并执行我们记录的动作视频如下:

今天的内容就到这里,感兴趣的童鞋可以动手测试一下,下节见。

关注公号

下面的是我的公众号二维码图片,欢迎关注。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 yale记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 关注公号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档