[Python crawler] Selenium Implementation Automatic login 163 mailbox and locating elements Introduction

Source: Internet
Author: User
Tags assert tag name xpath xpath contains

The first three articles describe the installation process and the ability to access the Firefox browser via selenium and automatically search for "Eastmount" keywords. This article mainly briefly describes how to implement automatic login 163 mailbox, while continuing to introduce Selenium+python official website locating elements part of the content.
Hope that the basic article is helpful to you, if there are errors or shortcomings, please Haihan ~
        [python crawler] install PHANTOMJS and Casperjs in Windows and introduction (top)
        [Python crawler] installs pip+phantomjs+selenium under Windows
[python crawler] selenium automatically access Firefox and Chrome and implement search


I. Selenium auto-Login

The code looks like this:

From selenium import webdriverfrom selenium.webdriver.common.keys import keysimport time# analog Login 163 Mailbox Driver = Webdriver. Firefox () driver.get ("http://mail.163.com/") #用户名 password Elem_user = driver.find_element_by_name ("username") elem_ User.send_keys ("15201615157") Elem_pwd = Driver.find_element_by_name ("password") Elem_pwd.send_keys ("********") Elem_pwd.send_keys (Keys.return) time.sleep (5) Assert "Baidu" in Driver.titledriver.close () Driver.quit ()
run the results as shown, automatically open the Firefox browser and enter a user name and password to implement the mailbox login.



The code is very simple, the principle is simple: access to the Firefox browser and URL via driver, while find_element_by_name find the corresponding value in the HTML source code and fill in the page, Finally call the keys to implement the simulation operation keyboard Keys.return implementation. The assertion result is non-existent and is primarily used to prevent the browser from being closed.
Typing the user name or password error will prompt, in fact, is the browser.

Explanation of principle
two years ago when I was learning C # network programming, I wrote the article WinForm automatic access to 163 mailboxes:
        C # Network Programming Web page automatic login (i). Use the Webbrower control to impersonate a login
By contrast, PythonShort and efficientThe advantages of the display, including 163 of the email login interface HTML Source code has not been modified and repaired, this is my unexpected.
The user name ID "idinput" is found by locating the login page, the password ID is "Pwdinput" and the login button ID is "LOGINBTN". ID and Name:
<input class= "" tabindex= "1" title= "Please enter account number " id= "Idinput" name= "username " type= "text" value= "".
<input class= "" tabindex= "2" title= "Please enter the password" id= "pwdinput" name= "Password " type= "password"/>
<button id= "loginbtn" class= "" type= "submit" > Login </button>
as shown at a glance:


Although this part of the article is simple, but as the basic article in the appropriate, at the same time through the Webdriver Driver.find_element_by_name lead to the following basic knowledge introduction, after all, practice example is to learn selenium power source.
Also the following code can implement automatic login csdn, is it possible to implement brute force password?
From selenium import webdriverfrom selenium.webdriver.common.keys import keysimport timedriver = Webdriver. Firefox () driver.get ("https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn") Elem_user = Driver.find_element_by_name ("username") Elem_user.send_keys ("Eastmount") Elem_pwd = Driver.find_element_by_name (" Password ") Elem_pwd.send_keys (" ******** ") Elem_pwd.send_keys (Keys.return) time.sleep (5) Assert" Baidu "in Driver.titledriver.close () Driver.quit ()



two. Locating elements Introduction
        ps: first upload translation blog, if there are errors please forgive me!
        official website address: http://selenium-python.readthedocs.org/locating-elements.html
        There are various strategies for locating elements in a Web page (locate elements), you can choose the most appropriate scenario, selenium provides a way to define the elements of a page:
    • 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
here's how to find multiple elements (these methods will return a list):
    • Find_elements_by_name
    • Find_elements_by_xpath
    • Find_elements_by_link_text
    • Find_elements_by_partial_link_text
    • Find_elements_by_tag_name
    • Find_elements_by_class_name
    • Find_elements_by_css_selector
In addition to the public methods given above, there are two private methods that are useful in the Page object locator. These two private methods are find_element and find_elements, usage examples:
From selenium.webdriver.common.by import bydriver.find_element (By.xpath, '//button[text () = "Some text"] ') driver.find _elements (By.xpath, '//button ')
These are the properties that can be obtained through the class:
id = "id" xpath = "XPath" link_text = "link text" partial_link_text = "PARTIAL link text" name = "name" tag_name = "TAG name" CL Ass_name = "Class NAME" Css_selector = "CSS SELECTOR"

1 Locating by Id

This feature is used when you know the id attribute of an element. With this method, the first positioned element is returned when the id attribute value is matched. If no element matches the ID value, a nosuchelementexception exception will be thrown. For example, refer to this page source code:

form form elements can be positioned as follows:
Login_form = driver.find_element_by_id (' LoginForm ')

2 locating by Name

Use this method when you know the Name property of an element. By this method, the first element that satisfies the value of the Name property will be matched back, and if no element matches, a Nosuchelementexception exception will be thrown. For example, refer to the following source code:

Here's how to locate the Username&password element:
Username = driver.find_element_by_name (' username ') password = driver.find_element_by_name (' password ')
The "Login " login button is given before the "Clear" button:
Continue = Driver.find_element_by_name (' Continue ')

3 Locating by XPath
XPath is the language used to locate nodes in an XML document. Just as HTML can be an implementation of XML (XHTML), selenium users can use this powerful language to keep track of elements in a Web application. The XPath extension has exceeded (and supported) The simple method of locating by ID or name attribute, and has developed various new possibilities, such as locating the third check box (checkbox) on the page.
One of the main reasons for using XPath is that when you do not have an appropriate ID or name attribute to locate the element you need to locate, you can use XPath to locate the absolute element (not recommended), or to locate an element that has an ID or name attribute. The XPath locator can also specify elements through other, more than the ID and name attributes.
an absolute XPath contains all elements that are positioned, from the root (HTML) to its result, which may fail only slightly to the application. By locating the ID of an element nearby or the name attribute (the ideal parent element), you can navigate to the element you are tracking based on the relationship between them. This is unlikely to change and will make your test more robust. For example, refer to the following source code:
this form form element may be positioned as follows:
Login_form = Driver.find_element_by_xpath ("/html/body/form[1]") Login_form = Driver.find_element_by_xpath ("//form[1 ] ") Login_form = Driver.find_element_by_xpath ("//form[@id = ' loginform '] ")
[1] Absolute path (if the HTML has a slight change, it will be destroyed)
[2] The first FORM element in HTML
[3] A FORM element with a value of loginform that has a property name of ID
Here's how to locate the username element:
Username = Driver.find_element_by_xpath ("//form[input/@name = ' username ']") Username = Driver.find_element_by_xpath (" form[@id = ' loginform ']/input[1] ") Username = Driver.find_element_by_xpath ("//input[@name = ' username '] ")
[1] The first FORM element is implemented by an input child element, the Name property and the value username
[2] The first input child element is found by a form element of the Id=loginform value
[3] The first INPUT element with the name of the property named name and a value of username
Here's how to locate the "Clear" button element:
Clear_button = Driver.find_element_by_xpath ("//input[@name = ' Continue ' [@type = ' button ']") Clear_button = Driver.find _element_by_xpath ("//form[@id = ' loginform ']/input[4]")
[1] The property is named name with the value continue and the property name is type input control whose value is button
[2] Property id=loginform The fourth input child element of a FORM element
The above examples involve some basic knowledge, please refer to the following suggestions for more details:
    • W3Schools XPath Tutorial
    • The recommendation XPath
    • XPath Tutorial -With interactive examples.
There are also several very useful add-ons that can help to discover the XPath of an element:
    • XPath Checker -suggests XPath and can is used to test XPath results.
    • Firebug -XPath suggestions is just one of the many powerful features of this very useful add-on.
    • XPath Helper -for Google Chrome

4 Locating Hyperlinks by Link Text
Use this method when you know that the link text is used within an anchor tag. With this strategy, the first element that matches the link text value will be returned. If no element matches the link text, a Nosuchelementexception exception is thrown. The source code for the sample is as follows:
This continue.html link is positioned in the following way, and partial indicates a partial match:
Continue_link = Driver.find_element_by_link_text (' Continue ') Continue_link = Driver.find_element_by_partial_link_ Text (' Conti ')


5 Locating Elements by Tag Name
You can use this method when you want to locate an element by using the tag name (tag name). Similarly, the first given tag name element will be returned, and if there is no matching tag name, a Nosuchelementexception exception will be thrown. The source code for the sample is as follows:
the methods for locating heading (H1) elements are as follows:
Heading1 = Driver.find_element_by_tag_name (' H1 ')


6 Locating Elements by Class Name
describes a similar, used for locating an element through the Class property name (attribute name). The sample source code is as follows:
where the element "P" is positioned as follows:
Content = driver.find_element_by_class_name (' content ')


7 locating Elements by CSS selectors
You can use this method when you want to position an element with CSS selector syntax. It returns the first element that matches the CSS selector, and returns an nosuchelementexception exception if there are no elements matching the CSS selector. The instance source code looks like this:
where the element "P" is positioned as follows:
Content = Driver.find_element_by_css_selector (' p.content ')
The sauce Lab has very good documentation on CSS selectors:
Sauce Labs has good documentationOn CSS selectors.

(By:eastmount 2015-8-21 6 o'clock in the afternoon http://blog.csdn.net/eastmount/)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Python crawler] Selenium Implementation Automatic login 163 mailbox and locating elements Introduction

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.