代码如下所示:
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time #模拟登陆163邮箱 driver = webdriver.Firefox() driver.get("http://mail.163.com/") #用户名 密码 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.title driver.close() driver.quit()运行结果如下图所示,自动打开Firefox浏览器并输入用户名和密码实现邮箱登录。
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time driver = 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.title driver.close() driver.quit()
from selenium.webdriver.common.by import By driver.find_element(By.XPATH, '//button[text()="Some text"]') driver.find_elements(By.XPATH, '//button')这些都是通过类可获取的属性:
ID = "id" XPATH = "xpath" LINK_TEXT = "link text" PARTIAL_LINK_TEXT = "partial link text" NAME = "name" TAG_NAME = "tag name" CLASS_NAME = "class name" 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')
<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>这个表单form元素可能通过如下方法被定位:
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] 绝对路径(如果HTML有稍微的改动,就会被破坏)
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] 第一个form元素通过一个input子元素,name属性和值为username实现
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] 属性名为name其值为continue和属性名为type其值为button的Input控件
<html> <body> <p>Are you sure you want to do this?</p> <a href="continue.html">Continue</a> <a href="cancel.html">Cancel</a> </body> <html>这个continue.html链接定位的方法如下,partial表示部分匹配:
continue_link = driver.find_element_by_link_text('Continue') continue_link = driver.find_element_by_partial_link_text('Conti')
<html> <body> <h1>Welcome</h1> <p>Site content goes here.</p> </body> <html>定位heading(h1)元素的方法如下:
heading1 = driver.find_element_by_tag_name('h1')
<html> <body> <p class="content">Site content goes here.</p> </body> <html>其中元素"p"的定位方法如下:
content = driver.find_element_by_class_name('content')
<html> <body> <p class="content">Site content goes here.</p> </body> <html>其中元素"p"的定位方法如下:
content = driver.find_element_by_css_selector('p.content')Sauce实验室有非常好的关于CSS选择器的文档: