The captain takes you on a book-"Selenium2 Python Automation Test" (2) Browser operation

Source: Internet
Author: User
Tags tag name xpath

Browser actions
# coding: utf-8from selenium import webdriverfrom time import sleepdriver = webdriver.Firefox()driver.get("https://www.baidu.com")# 设置浏览器宽800,高400driver.set_window_size(800, 400)sleep(3)# 最大化窗口driver.maximize_window()driver.get("https://www.cnblogs.com/")sleep(3)# 后退到上一个页面driver.back()sleep(3)# 前进到下一个页面driver.forward()sleep(3)# 退出浏览器driver.quit()
Object Positioning

Webdriver provides a range of element positioning methods, which are commonly used in the following ways:

    • Id

    • Name

    • Class name

    • Tag name

    • Link text

    • Partial link text

    • Xpath

    • CSS Selector

The methods that correspond to Python webdriver are:

    • FIND_ELEMENT_BY_ID ()

    • Find_element_by_name ()

    • Find_element_by_class_name ()

    • Find_element_by_tag_name ()

    • Find_element_by_link_text ()

    • Find_element_by_partial_link_text ()

    • Find_element_by_xpath ()

    • Find_element_by_css_selector ()

      Let's take blog.hexun.com as an example:

      This is his login box. Press F12 or right mouse button to select Inspect in Firepath Firefox will pop up to view the element interface:

      Click on the arrows as shown there, then go to the page where you want to locate the element, for example we now want to locate the input account box, we will click on the view element of the icon, and then click on the Input Account box, you can see the view element of the page is located here:

      Here, the page element property has ID, name, and so on, which is where we can locate this box by ID (find_element_by_id) or name (Find_element_by_name), generally name or class_name,tag_ Name these element names are not unique, possibly called DIV (tag_name) Another place also called Div, here is called name= "username", another place name is also username, this time with Name,class_name,tag_ Name is very difficult to locate the element you want, so, do not hesitate, not too long to use XPath or Css_selector bar, if there is ID can also, because generally the ID is definitely unique.
      How does name locate XPath or css_selector? That's the view page element there selection

      Firepath that tag, already see the XPath of the account bar, just click on the small triangle behind the XPath, you can switch to CSS (that is, Css_selector) positioning method:

      So, in the future when the positioning of the time basically without thinking, directly with Firepath This tool can be.

    • Public search " Automated test practice" or scan the QR code below to add attention ~ ~ ~

The captain takes you on a book-"Selenium2 Python Automation Test" (2) Browser operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.