3

Selenium简明教程

 1 year ago
source link: https://www.qixinbo.info/2023/04/22/selenium/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Selenium简明教程

(以下内容全部来自ChatGPT)

Selenium是一个自动化测试工具,主要用于测试Web应用程序的功能和性能。下面是一些Selenium的基本概念:

  1. 浏览器驱动
    Selenium需要使用浏览器驱动来控制浏览器。驱动程序是一个可执行文件,它能够和特定版本的浏览器进行交互。Selenium支持多种浏览器,例如Chrome、Firefox、Safari、IE等,每种浏览器都需要对应的驱动程序。
  2. 元素
    在Web页面中,所有的内容都是由元素组成的。元素可以是文本、按钮、链接、下拉列表等。在Selenium中,您可以使用元素对象来查找和操作网页上的元素。
  3. 定位器
    在Selenium中,您可以使用定位器来查找元素。定位器可以按ID、按名称、按类名、按标签名等方式来查找元素。例如,可以使用find_element_by_id方法按ID查找元素,使用find_element_by_name方法按名称查找元素。
  4. 动作
    在Selenium中,您可以使用元素对象的方法来执行一些动作。例如,可以使用send_keys方法向文本框中输入文本,使用click方法点击按钮等。
  5. 断言
    在测试中,您需要对测试结果进行断言,即验证结果是否符合预期。在Selenium中,您可以使用assert语句来进行断言。例如,可以使用assert “Selenium” in driver.title语句来断言页面标题是否包含指定文本。
  6. 测试框架
    在Selenium中,您可以使用测试框架来管理测试用例和执行测试。Python中常用的测试框架包括unittest和pytest。测试框架可以帮助您编写和执行测试,同时还提供了一些辅助方法和报告功能。

下面是入门 Selenium 的步骤:

  1. 安装 Selenium
    Selenium 可以通过 pip 安装:
    pip install selenium
    同时,还需要下载浏览器驱动程序,比如 Chrome 驱动程序。可以在 Selenium 官网上下载对应的驱动程序。
    在下载了浏览器驱动程序后,需要将其添加到系统的环境变量中,以便 Selenium 在启动浏览器时可以找到对应的驱动程序。
  2. 创建浏览器实例
    在 Python 中,可以使用 webdriver 模块来创建浏览器实例。比如,以下代码可以创建一个 Chrome 浏览器实例:
    from selenium import webdriver

    driver = webdriver.Chrome()
  3. 打开网页
    使用 get() 方法可以打开指定的网页:
    driver.get('https://www.baidu.com/')
  4. 查找元素
    使用 find_element_by_*() 方法可以查找页面上的元素,比如通过 ID、名称、标签名等查找。以下是一些示例:
    # 通过 ID 查找元素
    element = driver.find_element_by_id('element-id')

    # 通过名称查找元素
    element = driver.find_element_by_name('element-name')

    # 通过标签名查找元素
    element = driver.find_element_by_tag_name('element-tag')
  5. 操作元素
    一旦找到了页面上的元素,就可以对其进行操作。比如,以下代码可以在百度搜索框中输入关键词并提交搜索:
    search_box = driver.find_element_by_id('kw')
    search_box.send_keys('Python')
    search_box.submit()
  6. 关闭浏览器
    使用 quit() 方法可以关闭浏览器:
    driver.quit()
    这些是 Selenium 的基本用法,可以用于自动化浏览器操作。需要注意的是,Selenium 可能会被一些网站检测到,因此在使用时需要注意不要违反相关规定。

Selenium提供了许多鼠标操作的方法,可以通过鼠标模拟用户的操作。以下是一些常用的鼠标操作方法:

  1. 鼠标单击(click):
    from selenium.webdriver.common.action_chains import ActionChains

    # 找到元素
    element = driver.find_element_by_xpath("//xpath/to/element")

    # 单击元素
    ActionChains(driver).click(element).perform()
  2. 鼠标双击(double_click):
    from selenium.webdriver.common.action_chains import ActionChains

    # 找到元素
    element = driver.find_element_by_xpath("//xpath/to/element")

    # 双击元素
    ActionChains(driver).double_click(element).perform()
  3. 鼠标右键单击(context_click):
    from selenium.webdriver.common.action_chains import ActionChains

    # 找到元素
    element = driver.find_element_by_xpath("//xpath/to/element")

    # 右键单击元素
    ActionChains(driver).context_click(element).perform()
  4. 鼠标拖动(drag_and_drop):
    from selenium.webdriver.common.action_chains import ActionChains

    # 找到元素
    source_element = driver.find_element_by_xpath("//xpath/to/source_element")
    target_element = driver.find_element_by_xpath("//xpath/to/target_element")

    # 拖动元素
    ActionChains(driver).drag_and_drop(source_element, target_element).perform()
  5. 鼠标移动(move_to_element):
    from selenium.webdriver.common.action_chains import ActionChains

    # 找到元素
    element = driver.find_element_by_xpath("//xpath/to/element")

    # 移动鼠标到元素上
    ActionChains(driver).move_to_element(element).perform()
  6. 鼠标移动到指定位置(move_by_offset):
    from selenium.webdriver.common.action_chains import ActionChains

    # 移动鼠标到相对于当前位置的偏移量
    ActionChains(driver).move_by_offset(x_offset, y_offset).perform()
    其中,x_offsety_offset是相对于当前鼠标位置的偏移量。
    这些鼠标操作方法可以用于模拟用户的操作,例如在网页上单击、拖动、右键单击等。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK