📜  Selenium Python教程

📅  最后修改于: 2022-05-13 01:54:21.146000             🧑  作者: Mango

Selenium Python教程

Selenium是一个强大的工具,用于通过程序控制 Web 浏览器并执行浏览器自动化。它适用于所有浏览器,适用于所有主要操作系统,其脚本是用各种语言编写的,例如Python、 Java、C# 等,我们将使用Python。 Selenium教程涵盖了所有主题,例如 - WebDriver、WebElement、使用selenium进行单元测试。本Python Selenium教程涵盖了从基础到高级和专业用途的Selenium 。

Selenium-Python-教程

为什么要学习Selenium Python ?

  • 开源和可移植Selenium是一个开源和可移植的 Web 测试框架。
  • 工具和 DSL的结合Selenium是工具和 DSL(领域特定语言)的结合,以进行各种类型的测试。
  • 更容易理解和实现Selenium命令按照不同的类进行分类,这使得它更容易理解和实现。
  • 减少测试人员的负担和压力——如上所述,在每个新版本上测试重复测试场景所需的时间几乎减少到零。因此,减少了测试人员的负担。
  • 为企业客户降低成本——企业需要向测试人员支付他们的薪水,这些薪水是使用自动化测试工具节省下来的。自动化不仅可以节省时间,还可以为企业带来成本效益。
Selenium基础知识
  • Selenium基础知识
  • Selenium的成分
  • 应用和用途
  • 特征
  • 限制

SeleniumPython基础知识

  • Selenium Python介绍及安装
  • 使用 get 方法导航链接
  • 与网页交互
  • 定位单个元素
  • 定位多个元素
  • 定位器策略 – Selenium Python
  • 使用Selenium Python编写测试

定位策略

  • 定位单个元素 -
    • find_element_by_id()
    • find_element_by_name()
    • find_element_by_xpath()
    • find_element_by_link_text()
    • find_element_by_partial_link_text()
    • find_element_by_tag_name()
    • find_element_by_class_name()
    • find_element_by_css_selector()
  • 定位多个元素 -
    • find_elements_by_name()
    • find_elements_by_xpath()
    • find_elements_by_link_text()
    • find_element_by_partial_link_text()
    • find_elements_by_tag_name()
    • find_elements_by_class_name()
    • find_elements_by_css_selector()

等待

  • 显式等待
  • 隐式等待

动作链

  • 动作链基础
  • 点击
  • 点击并按住
  • context_click
  • 双击
  • 拖放
  • key_down
  • key_up
  • move_by_offset
  • move_to_element
  • move_to_element_with_offset
  • 释放
  • 重置动作
  • 发送键

Selenium Python高级 -

  • 处理异常Selenium Python
  • Selenium Python中的特殊键
  • 如何处理Selenium Python中的警报提示?
  • 在Selenium Python中添加和删除 Cookie
  • 如何使用Selenium Python在历史中前后移动?
  • Selenium Python中的特殊键
  • 使用 TestNg 在Selenium WebDriver 中断言
  • Selenium Python技巧
  • 页面对象模型 (POM)

项目示例

  • Whatsapp 使用Python!
  • 使用Selenium 的浏览器自动化
  • 使用Python登录 Facebook
  • 使用Selenium在 Facebook 上自动发布生日快乐帖子
  • 如何使用Python在selenium中访问弹出登录窗口
  • 使用Selenium的短信轰炸机

Selenium网络驱动程序


Selenium Webdriver 是Selenium Python中使用的所有方法和类的父级。正是Selenium的驱动力使我们能够对网页上的多个元素执行各种操作。 Driver 具有多种方法和属性,可用于在Selenium Python中进行自动化测试。要查看如何使用 webdriver,请访问 – Selenium Python中的 Web 驱动程序。可以在selenium Python中使用的各种方法是 -
MethodDescription

add_cookieAdds a cookie to your current session.
backGoes one step backward in the browser history.
closeCloses the current window.
create_web_elementCreates a web element with the specified element_id.
delete_all_cookiesDelete all cookies in the scope of the session.
delete_cookieDeletes a single cookie with the given name.
execute_async_scriptAsynchronously Executes JavaScript in the current window/frame.
execute_scriptSynchronously Executes JavaScript in the current window/frame.
forwardGoes one step forward in the browser history.
fullscreen_windowInvokes the window manager-specific ‘full screen’ operation
get_cookieGet a single cookie by name. Returns the cookie if found, None if not.
get_cookiesReturns a set of dictionaries, corresponding to cookies visible in the current session.
get_logGets the log for a given log type
get_screenshot_as_base64Gets the screenshot of the current window as a base64 encoded string which is useful in embedded images in HTML.
get_screenshot_as_fileSaves a screenshot of the current window to a PNG image file.
get_screenshot_as_pngGets the screenshot of the current window as a binary data.
get_window_positionGets the x, y position of the current window.
get_window_rectGets the x, y coordinates of the window as well as height and width of the current window.
get_window_sizeGets the width and height of the current window.
implicitly_waitSets a sticky timeout to implicitly wait for an element to be found,
maximize_windowMaximizes the current window that webdriver is using
minimize_windowInvokes the window manager-specific ‘minimize’ operation
quitQuits the driver and closes every associated window.
refreshRefreshes the current page.
set_page_load_timeoutSet the amount of time to wait for a page load to complete before throwing an error.
set_script_timeoutSet the amount of time that the script should wait during an execute_async_script call before throwing an error.
set_window_positionSets the x, y position of the current window. (window.moveTo)
set_window_rectSets the x, y coordinates of the window as well as height and width of the current window.
current_urlGets the URL of the current page.
current_window_handleReturns the handle of the current window.
page_sourceGets the source of the current page.
titleReturns the title of the current page.

SeleniumWebElement

元素可以是标签、属性或任何东西,它是类selenium.webdriver.remote.webelement.WebElement的实例。使用selenium在屏幕上找到元素后,您可能希望单击它或查找子元素等Selenium提供了围绕Selenium的 WebElement 的方法。要查看如何在selenium中使用元素对象,请访问 – Selenium Python中的 WebElement。下面讨论了可以在Selenium Python中使用元素的各种方法 -
Element MethodsDescription
is_selected()is_selected method is used to check if element is selected or not. It returns a boolean value True or False.
is_displayed()is_displayed method is used to check if element it visible to user or not. It returns a boolean value True or False.
is_enabled()is_enabled method is used to check if element is enabled or not. It returns a boolean value True or False.
get_property()get_property method is used to get properties of an element, such as getting text_length property of anchor tag.
get_attribute()get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag.
send_keys()send_keys method is used to send text to any field, such as input field of a form or even to anchor tag paragraph, etc.
click()click method is used to click on any element, such as an anchor tag, a link, etc.
clear()clear method is used to clear text of any field, such as input field of a form or even to anchor tag paragraph, etc.
screenshot()screenshot method is used to save a screenshot of current element to a PNG file.
submit()submit method is used to submit a form after you have sent data to a form.
value_of_css_property()value_of_css_property method is used to get value of a css property for a element.
locationlocation method is used to get location of element in renderable canvas.
screenshot_as_pngscreenshot_as_png method is used to gets the screenshot of the current element as binary data.
parentparent method is used to get internal reference to the WebDriver instance this element was found from.
sizesize method is used to get size of current element.
tag_nametag_name method is used to get name of tag you are referring to.
texttext method is used to get text of current element.
rectrect method is used to get a dictionary with the size and location of the element.
screenshot_as_base64screenshot_as_base64 method is used to gets the screenshot of the current element as a base64 encoded string.