1、
通过示例介绍Selenium-WebDriver
一个简单的入门方法就是这个例子,
它在Google上搜索术语“Cheese”,
然后将结果页面的标题输出到控制台。
java csharp python
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
# 可自从2.4.0
from selenium.webdriver.support import expected_conditions as EC
# 可自从2.26.0
driver = webdriver.Firefox()
# 创建Firefox的驱动的一个新实例
driver.get("https://www.google.com")
# 去谷歌主页
print driver.title
# 页面Ajaxy所以标题原来是这样:
inputElement = driver.find_element_by_name("q")
# 找到元素的name属性是Q(谷歌搜索框)
inputElement.send_keys("cheese!")
# 键入搜索
inputElement.submit()
# 提交表单(尽管谷歌自动搜索现在没有提交)
try:
WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))
# 我们不得不等待页面刷新,最后一件事,似乎被更新的标题是
print driver.title
# 您应该看到"cheese! - Google Search"
finally:
driver.quit()
2、
抓取页面
您可能想要使用WebDriver做的第一件事是导航到一个页面。
正常的做法是调用“get”:
driver.get("https://www.google.com")
3、
查找UI元素(WebElements)
“Find”方法使用名为“By”的定位器或查询对象。
“By”策略列在下面。
By ID
如何找到如下所示的元素的示例:
<div id="coolestWidgetEvah">...</div>
element = driver.find_element_by_id("coolestWidgetEvah")
or
from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")
By Class Name
如何找到如下所示的元素的示例:
<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
cheeses = driver.find_elements_by_class_name("cheese")
or
from selenium.webdriver.common.by import By
cheeses = driver.find_elements(By.CLASS_NAME, "cheese")
By Tag Name
如何找到如下所示的元素的示例:
<iframe src="..."></iframe>
frame = driver.find_element_by_tag_name("iframe")
or
from selenium.webdriver.common.by import By
frame = driver.find_element(By.TAG_NAME, "iframe")
By Name
如何找到如下所示的元素的示例:
<input name="cheese" type="text"/>
cheese = driver.find_element_by_name("cheese")
or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.NAME, "cheese")
By Link Text
如何找到如下所示的元素的示例:
<a href="http://www.google.com/search?q=cheese">cheese</a>>
cheese = driver.find_element_by_link_text("cheese")
or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.LINK_TEXT, "cheese")
By Partial Link Text
如何找到如下所示的元素的示例:
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
cheese = driver.find_element_by_partial_link_text("cheese")
or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")
By CSS
下面找到奶酪的例子:
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")
or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")
By XPath
这是一个小抽象,所以对于下面的一段HTML:
<input type="text" name="example" />
<INPUT type="text" name="other" />
inputs = driver.find_elements_by_xpath("//input")
or
from selenium.webdriver.common.by import By
inputs = driver.find_elements(By.XPATH, "//input")
Using JavaScript
jQuery加载的页面上的简单示例:
element = driver.execute_script("return $(‘.cheese‘)[0]")
为页面上的每个标签查找所有输入元素:
labels = driver.find_elements_by_tag_name("label")
inputs = driver.execute_script(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute(‘for‘))); } return inputs;", labels)