前三篇文章介绍了安装过程和经过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选择器的文档: