前三篇文章介紹了安裝過程和經過Selenium實現訪問Firefox瀏覽器並自動搜索"Eastmount"關鍵字及截圖的功能。而這篇文章主要簡單介紹如何實現自動登陸163郵箱,同時繼續介紹Selenium+Python官網Locating Elements部份內容。
但願該篇基礎性文章對你有所幫助,若是有錯誤或不足之處,請海涵~
[Python爬蟲] 在Windows下安裝PhantomJS和CasperJS及入門介紹(上)
[Python爬蟲] 在Windows下安裝PIP+Phantomjs+Selenium
[Python爬蟲] Selenium自動訪問Firefox和Chrome並實現搜索截圖
注意:好像訪問瀏覽器在C盤會自動生成文件愈來愈小,但能夠清理,不知道爲啥?
css
代碼以下所示:html
1 from selenium import webdriver 2 from selenium.webdriver.common.keys import Keys 3 import time 4 5 #模擬登錄163郵箱 6 driver = webdriver.Firefox() 7 driver.get("http://mail.163.com/") 8 9 #用戶名 密碼 10 elem_user = driver.find_element_by_name("username") 11 elem_user.send_keys("15201615157") 12 elem_pwd = driver.find_element_by_name("password") 13 elem_pwd.send_keys("********") 14 elem_pwd.send_keys(Keys.RETURN) 15 time.sleep(5) 16 assert "baidu" in driver.title 17 driver.close() 18 driver.quit()
運行結果以下圖所示,自動打開Firefox瀏覽器並輸入用戶名和密碼實現郵箱登陸。python
1 from selenium import webdriver 2 from selenium.webdriver.common.keys import Keys 3 import time 4 5 driver = webdriver.Firefox() 6 driver.get("https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn") 7 elem_user = driver.find_element_by_name("username") 8 elem_user.send_keys("Eastmount") 9 elem_pwd = driver.find_element_by_name("password") 10 elem_pwd.send_keys("********") 11 elem_pwd.send_keys(Keys.RETURN) 12 time.sleep(5) 13 assert "baidu" in driver.title 14 driver.close() 15 driver.quit()
PS:第一次上傳翻譯博文,若是有錯誤還請見諒!
官網地址:http://selenium-python.readthedocs.org/locating-elements.html
這裏有各類策略用於定位網頁中的元素(locate elements),你能夠選擇最適合的方案,Selenium提供了一下方法來定義一個頁面中的元素:web
下面是查找多個元素(這些方法將返回一個列表):chrome
除了上面給出的公共方法,這裏也有兩個在頁面對象定位器有用的私有方法。這兩個私有方法是find_element和find_elements,用法示例:編程
1 from selenium.webdriver.common.by import By 2 3 driver.find_element(By.XPATH, '//button[text()="Some text"]') 4 driver.find_elements(By.XPATH, '//button')
這些都是經過類可獲取的屬性:瀏覽器
1 ID = "id" 2 XPATH = "xpath" 3 LINK_TEXT = "link text" 4 PARTIAL_LINK_TEXT = "partial link text" 5 NAME = "name" 6 TAG_NAME = "tag name" 7 CLASS_NAME = "class name" 8 CSS_SELECTOR = "css selector"
當你知道一個元素的id屬性時使用該功能。有了這個方法,用id屬性值匹配時第一個被定位的元素將被返回。若是沒有元素匹配id值,一個NoSuchElementException異常將會拋出。例如,參考這個頁面源碼:網絡
<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> </form> </body> <html>
表單form元素能夠被以下方式定位:學習
login_form = driver.find_element_by_id('loginForm')
當你知道一個元素的name屬性時使用該方法。經過該方法,第一個知足name屬性值的元素將被匹配返回,若是沒有元素匹配,將拋出一個NoSuchElementException異常。例如,參考下面源碼:測試
<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> <html>
定位username&password元素方法以下:
username = driver.find_element_by_name('username') password = driver.find_element_by_name('password')
在"Clear"按鈕以前會給出"Login"登陸按鈕:
continue = driver.find_element_by_name('continue')
XPath是用於定位XML文檔中節點的語言。正如HTML能夠是XML(XHTML)的一個實現,Selenium用戶能夠利用這個強大的語言來跟蹤Web應用程序中的元素。XPath擴展已經超出(以及支持)了按照id或name屬性定位的簡單方法,並開發了各類新的可能,如定位頁面上的第三個複選框(checkbox)。
其中使用XPath的一個主要緣由是:當你沒有一個合適的ID或Name屬性來定位你須要查找的元素時,你能夠使用XPath去定位這個絕對元素(不建議這樣),或者相對一個有id或name屬性的元素定位。XPath定位器也能夠經過其餘不止是id和name屬性進行指定元素。
絕對XPath包含定位的全部元素,這些元素從根(HTML)到其結果可能會失敗,只有稍微調整到應用程序。經過找到附近的一個元素的id或name屬性(理想的父元素),你才能夠根據之間的關係定位到你追蹤的元素。這是不太可能改變的,而且會使你的測試更加的健壯。例如參考下面這段源代碼:
1 <html> 2 <body> 3 <form id="loginForm"> 4 <input name="username" type="text" /> 5 <input name="password" type="password" /> 6 <input name="continue" type="submit" value="Login" /> 7 <input name="continue" type="button" value="Clear" /> 8 </form> 9 </body> 10 <html>
1 login_form = driver.find_element_by_xpath("/html/body/form[1]") 2 login_form = driver.find_element_by_xpath("//form[1]") 3 login_form = driver.find_element_by_xpath("//form[@id='loginForm']")
1 username = driver.find_element_by_xpath("//form[input/@name='username']") 2 username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]") 3 username = driver.find_element_by_xpath("//input[@name='username']")
1 clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']") 2 clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
1 <html> 2 <body> 3 <p>Are you sure you want to do this?</p> 4 <a href="continue.html">Continue</a> 5 <a href="cancel.html">Cancel</a> 6 </body> 7 <html>
1 continue_link = driver.find_element_by_link_text('Continue') 2 continue_link = driver.find_element_by_partial_link_text('Conti')
1 <html> 2 <body> 3 <h1>Welcome</h1> 4 <p>Site content goes here.</p> 5 </body> 6 <html>
heading1 = driver.find_element_by_tag_name('h1')
1 <html> 2 <body> 3 <p class="content">Site content goes here.</p> 4 </body> 5 <html>
content = driver.find_element_by_class_name('content')
1 <html> 2 <body> 3 <p class="content">Site content goes here.</p> 4 </body> 5 <html>
content = driver.find_element_by_css_selector('p.content')Sauce實驗室有很是好的關於CSS選擇器的文檔: