WebDriver-Simple Element operations, webdriver-Elements
The following describes how to log on to the 163 mailbox:
① Switch_to.frame ()
②. Clear ()
③. Send_keys ()
④. Click ()
⑤ Switch_to_default_content ()
I. Analyze the HTML code of the 163 mailbox logon homepage as follows, and analyze several notes
Use the Firefox browser to locate the "email account" input through Firebug. Its html code is like the blue part.
This element is analyzed first. Its tag is input, and its attributes include id, class, type, name, style, etc,
It is not difficult to find that the element's id attribute value contains numbers. Generally, the id value with numbers is variable and not unique. For the sake of insurance, you do not need to use the id method to locate the element.
At the top of the email address input box, we can see that it is nested in the Form frame/iframe. Therefore, when locating the email account input box, we first enter the frame/iframe form,
There are three methods to locate frame/iframe: id, name, and xpath.
For example: switch_to_frame (id); or switch_to_frame (name); if the frame does not have an id or name, it can only be located in the frame through xpath, switch_to_frame (xpath)
II. The specific logon code is as follows:
#-*-Coding: UTF-8-*-from selenium import webdriverimport timedriver = webdriver. firefox () driver. get ("http://www.mail.163.com/") driver. switch_to.frame ("x-URS-iframe") # ① switch_to.frame () enter the driver in the form. find_element_by_xpath ("// input [@ class = 'J-inputtext dlemail']"). clear () # ② locate the email address input box. Sometimes the previous account is remembered in the input box, so clear the content driver in the input box with clear. find_element_by_xpath ("// input [@ class = 'J-inputtext dlemail']"). send_keys (username) # ③ use send_keys () in the email address input box to enter the user name driver. find_element_by_xpath ("// input [@ class = 'J-inputtext dlpwd']"). send_keys (password) # ③ use send_keys () in the password input box to enter the password driver. find_element_by_id ("dologin "). click () # ④ click () and click the login button driver. switch_to.default_content () # ⑤ switch_to.default_content () Exit all forms; switch_to.parent_frame () Exit the current first-level form time. sleep (5) driver. quit ()
If the content in this article is incorrect, I hope you can correct it. Thank you.