例子demo
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
# 打開瀏覽器
URL = 'https://www.baidu.com'
browser = webdriver.Firefox() # 打開瀏覽器
# 設置等待加載時間
wait = WebDriverWait(browser, 5)
browser.get(URL)
wait.until(Ec.presence_of_element_located((By.ID,'kw')))
# 獲取網頁中的元素,填充數據以及點擊事件
browser.find_element_by_id("kw").send_keys("selenium")
browser.find_element_by_id("su").click()
browser.quit()
設置瀏覽器的地址
browser = webdriver.Firefox(executable_path="瀏覽器的地址")
瀏覽器的基本操作
最大化: browser.maximize_window()
彈窗: browser.switch_to_alert()
刷新: browser.forward()
后退: browser.backward()
刷新: browser.refresh()
關閉: browser.close() # 關閉當前窗口
退出: browser.quit()
在瀏覽器中找到的元素叶撒,webdriver提供了一系列的對象定位方法,browser.find_element_by_屬性()诫硕,常用的屬性有以下幾種:
· id
· name
· class_name
· link_text
· partial_link_text
· tag_name
· xpath
· css_selector
在瀏覽器中找到頁面元素以后,對它做的一些操作:
WebElement 類
- 點擊該元素: click()
- 清除該元素原有的文字: clear()
- 給該元素填寫新的文字: send_keys()
- 獲取該元素的文字: text
- 獲取該元素的指定屬性: get_attribute()
- 獲取當前元素的某一種屬性:getAttribute(attribute)
- 獲取當前元素的文字屬性:getText()
- 獲取當前元素的 Displayed 屬性: isDisplayed()
selenium.webdriver.common.by.By類熄浓,By的后面可選的屬性:
CLASS_NAME = 'class name'
CSS_SELECTOR = 'css selector'
ID = 'id'
LINK_TEXT = 'link text'
NAME = 'name'
PARTIAL_LINK_TEXT = 'partial link text'
TAG_NAME = 'tag name'
XPATH = 'xpath'
browser.find_element(By.ID, "q")
selenium.webdriver.support.expected_conditions類寻行,
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as Ec
Ec.presence_of_element_located((By.ID,'kw'))
Ec.presence_of_all_elements_located((By.ID, 'query'))
Ec.element_to_be_clickable()
Ec.title_is('title')
Ec.title_contains('title') # 判斷頁面標題包含title
# locator, eg: (By.ID, 'erwx')
Ec.visibility_of_element_located(locator) # 等待locator元素可見
Ec.invisibility_of_element_located(locator) # 等待locator元素隱藏
Ec.visibility_of(driver.find_element_by_link_text('高級搜索'))
Ec.text_to_be_present_in_element((By.ID, 'query'), 'text') # 等待locator的元素中包含text文本
Ec.text_to_be_present_in_element_value((By.ID, 'query'), 'selenium') # 等待locator元素的value屬性為value
Ec.frame_to_be_available_and_switch_to_it((By.ID, 'frame')) # 等待frame可切入
Ec.alert_is_present() # 等待alert彈出窗口出現(xiàn)
Ec.element_to_be_selected(element) # 等待element元素是被選中
Ec.element_selection_state_to_be(element, is_selected) #等待element元素的值被選中為is_selected(布爾值)
等待網頁加載
- 強制等待
一直等待到設置的時間后才執(zhí)行下一步
import time
time.sleep(5) # 等待5秒
- 隱式等待
最長等待設置的時間, 若在設置的時間內完成加載, 則可以在加載完后繼續(xù)執(zhí)行下一步
browser.implicitly_wait(30) # 隱性等待,等30秒
- 顯性等待
selenium.webdriver.support.wait.WebDriverWait感凤,配合該類的until()和until_not()方法悯周,能夠根據判斷條件而進行靈活地等待.
WebDriverWait(browser, timeout, poll_frequency, ignored_exceptions).until(可執(zhí)行方法, 超時時返回的信息)
timeout: 超時時間,等待的最長時間(同時要考慮隱性等待時間)
poll_frequency: 調用until或until_not中的方法的間隔時間陪竿,默認是0.5秒
ignored_exceptions: 忽略的異常队橙,如果在調用until或until_not的過程中拋出這個元組中的異常, 則不中斷代碼,繼續(xù)等待萨惑,如果拋出的是這個元組外的異常,則中斷代碼仇矾,拋出異常庸蔼。默認只有NoSuchElementException。
driver.implicitly_wait(10) # 隱性等待和顯性等待可以同時用贮匕,但要注意:等待的最長時間取兩者之中的大者
locator =(By.LINK_TEXT, 'link')
WebDriverWait(driver, 20, 0.5).until(Ec.presence_of_element_located(locator))
附一篇大佬寫的簡書, 非常全面: http://www.reibang.com/p/5b9fa11a9808