[Python]View PlainCopy
- <span style="font-family:arial, Helvetica, Sans-serif;" ><span style="FONT-SIZE:18PX;" ># coding:utf-8 #这句是为了声明编码格式, be sure to have </span></span>
[Python]View PlainCopy
- <span style="FONT-SIZE:18PX;" > fromSelenium import webdriver #要想使用selenium function in the Webdriver, first bring the package in
- Import time #调入time function
- Driver=webdriver. Ie ()
- #选择浏览器, can be Firefox, Ie or Chrome, need to install the browser plugin before use;
- #driver是一个变量, you can pick up.
- Driver.get ("http://m.mail.10086.cn")
- The #mplicitly_wait () method implements the smart wait, which is equivalent to the set point in Uft, where the smart wait is 30 seconds
- Driver.implicitly_wait (+)
- driver.find_element_by_id ("ur"). Send_keys ("15610537527")
- #一个元素有若干属性id, name, (or otherwise), where the element is identified by ID, the ID of the input box is called UR,
- #我要在输入框里输入15610537527
- driver.find_element_by_id ("PW"). Send_keys ("15866584957")
- #网页对象操作:
- #. Click () Tap Object
- #?. Send_keys ("xxx") simulates key input on an object
- Clear () Clears the contents of the input box, for example, the Baidu input box has a "Please enter a keyword" message by default.
- # again, for example, our login box will have default information such as "Account" and "password" in general.
- # clear can help us clear this information.
- #. Submit () Submission Form
- #. Text gets the literal of the element
- # Get_attribute ("property name, such as name") to get the property value
- Data1 = Driver.find_element_by_class_name ("loading_btn"). Text
- Print "This element text is:"
- Print Data1
- Data2 = Driver.find_element_by_class_name ("loading_btn"). Get_attribute ("id")
- Print "The element id attribute value is:"
- Print Data2
- Driver.find_element_by_class_name ("Loading_btn"). Click ()
- #搜索的按钮的name called loading_btn, I need to click the button (click ()).
- Time.sleep (5) #休眠5秒
- Print Driver.title # prints the page title
- Print ": page access is successful! "
[Python]View PlainCopy
- #将当前页面URL, print it out.
- Now_url = Driver.current_url
- Print Now_url
- Print "browser maximized:"
- Driver.maximize_window () #将浏览器窗口最大化显示
- Print "Set browser width 480, height 800 display:"
- Driver.set_window_size (480, )
- #操作浏览器访问网页后退
- Print "Back to Previous page:"
- Driver.back ()
- Time.sleep (1)
- #操作浏览器访问网页前进 (after back)
- Print "forward to Next page:"
- Driver.forward ()
- Time.sleep (2)
- #driver. Quit () #退出并关闭窗口的每一个相关的驱动程序
- Driver.close () #关闭当前窗口, pay attention to the difference between the above, select </span></span>
Selenium_webdriver (python) gets the element attribute value, browser window control, page forward backward, Title/url print