Selenium
視頻地址:
鏈接:https://pan.baidu.com/s/1RJETygOxiT1t2cSjFK_8cA密碼: 49ja
基本使用
fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromselenium.webdriver.common.keysimportKeysfromselenium.webdriver.supportimportexpected_conditionsasECfromselenium.webdriver.support.waitimportWebDriverWaitbrowser = webdriver.Chrome()try:? ? browser.get('https://www.baidu.com')? ? input = browser.find_element_by_id('kw')? ? input.send_keys('Python')? ? input.send_keys(Keys.ENTER)? ? wait = WebDriverWait(browser,10)? ? wait.until(EC.presence_of_element_located((By.ID,'content_left')))? ? print(browser.current_url)? ? print(browser.get_cookies())? ? print(browser.page_source)finally:? ? browser.close()
聲明瀏覽器對(duì)象
fromseleniumimportwebdriverbrowser = webdriver.Chrome()browser = webdriver.Firefox()browser = webdriver.Edge()browser = webdriver.PhantomJS()browser = webdriver.Safari()
訪問(wèn)頁(yè)面
fromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.get('https://www.taobao.com')print(browser.page_source)browser.close()
查找元素
單個(gè)元素
fromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.get('https://www.taobao.com')input_first = browser.find_element_by_id('q')input_second = browser.find_element_by_css_selector('#q')input_third = browser.find_element_by_xpath('//*[@id="q"]')print(input_first, input_second, input_third)browser.close()
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
fromseleniumimportwebdriverfromselenium.webdriver.common.byimportBybrowser = webdriver.Chrome()browser.get('https://www.taobao.com')input_first = browser.find_element(By.ID,'q')print(input_first)browser.close()
多個(gè)元素
fromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.get('https://www.taobao.com')lis = browser.find_elements_by_css_selector('.service-bd li')print(lis)browser.close()
fromseleniumimportwebdriverfromselenium.webdriver.common.byimportBybrowser = webdriver.Chrome()browser.get('https://www.taobao.com')lis = browser.find_elements(By.CSS_SELECTOR,'.service-bd li')print(lis)browser.close()
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
元素交互操作
對(duì)獲取的元素調(diào)用交互方法
fromseleniumimportwebdriverimporttimebrowser = webdriver.Chrome()browser.get('https://www.taobao.com')input = browser.find_element_by_id('q')input.send_keys('iPhone')time.sleep(1)input.clear()input.send_keys('iPad')button = browser.find_element_by_class_name('btn-search')button.click()
更多操作:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement
交互動(dòng)作
將動(dòng)作附加到動(dòng)作鏈中串行執(zhí)行
fromseleniumimportwebdriverfromselenium.webdriverimportActionChainsbrowser = webdriver.Chrome()url ='http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'browser.get(url)browser.switch_to.frame('iframeResult')source = browser.find_element_by_css_selector('#draggable')target = browser.find_element_by_css_selector('#droppable')actions = ActionChains(browser)actions.drag_and_drop(source, target)actions.perform()
更多操作:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains
執(zhí)行JavaScript
fromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.get('https://www.zhihu.com/explore')browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')browser.execute_script('alert("To Bottom")')
獲取元素信息
獲取屬性
fromseleniumimportwebdriverfromselenium.webdriverimportActionChainsbrowser = webdriver.Chrome()url ='https://www.zhihu.com/explore'browser.get(url)logo = browser.find_element_by_id('zh-top-link-logo')print(logo)print(logo.get_attribute('class'))
zu-top-link-logo
獲取文本值
fromseleniumimportwebdriverbrowser = webdriver.Chrome()url ='https://www.zhihu.com/explore'browser.get(url)input = browser.find_element_by_class_name('zu-top-add-question')print(input.text)
提問(wèn)
獲取ID胧奔、位置斥滤、標(biāo)簽名泄隔、大小
fromseleniumimportwebdriverbrowser = webdriver.Chrome()url ='https://www.zhihu.com/explore'browser.get(url)input = browser.find_element_by_class_name('zu-top-add-question')print(input.id)print(input.location)print(input.tag_name)print(input.size)
0.6822924344980397-1{'y': 7,'x': 774}button{'height': 32,'width': 66}
Frame
importtimefromseleniumimportwebdriverfromselenium.common.exceptionsimportNoSuchElementExceptionbrowser = webdriver.Chrome()url ='http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'browser.get(url)browser.switch_to.frame('iframeResult')source = browser.find_element_by_css_selector('#draggable')print(source)try:? ? logo = browser.find_element_by_class_name('logo')exceptNoSuchElementException:? ? print('NO LOGO')browser.switch_to.parent_frame()logo = browser.find_element_by_class_name('logo')print(logo)print(logo.text)
NO LOGORUNOOB.COM
等待
隱式等待
當(dāng)使用了隱式等待執(zhí)行測(cè)試的時(shí)候系草,如果 WebDriver沒(méi)有在 DOM中找到元素宵喂,將繼續(xù)等待框产,超出設(shè)定時(shí)間后則拋出找不到元素的異常, 換句話說(shuō)道批,當(dāng)查找元素或元素并沒(méi)有立即出現(xiàn)的時(shí)候荐吵,隱式等待將等待一段時(shí)間再查找 DOM骑冗,默認(rèn)的時(shí)間是0
fromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.implicitly_wait(10)browser.get('https://www.zhihu.com/explore')input = browser.find_element_by_class_name('zu-top-add-question')print(input)
顯式等待
fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasECbrowser = webdriver.Chrome()browser.get('https://www.taobao.com/')wait = WebDriverWait(browser,10)input = wait.until(EC.presence_of_element_located((By.ID,'q')))button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search')))print(input, button)
title_is 標(biāo)題是某內(nèi)容
title_contains? 標(biāo)題包含某內(nèi)容
presence_of_element_located 元素加載出,傳入定位元組先煎,如(By.ID, 'p')
visibility_of_element_located? 元素可見(jiàn)贼涩,傳入定位元組
visibility_of 可見(jiàn),傳入元素對(duì)象
presence_of_all_elements_located 所有元素加載出
text_to_be_present_in_element 某個(gè)元素文本包含某文字
text_to_be_present_in_element_value? 某個(gè)元素值包含某文字
frame_to_be_available_and_switch_to_it? frame加載并切換
invisibility_of_element_located 元素不可見(jiàn)
element_to_be_clickable? 元素可點(diǎn)擊
staleness_of? 判斷一個(gè)元素是否仍在DOM薯蝎,可判斷頁(yè)面是否已經(jīng)刷新
element_to_be_selected? 元素可選擇遥倦,傳元素對(duì)象
element_located_to_be_selected? 元素可選擇,傳入定位元組
element_selection_state_to_be? 傳入元素對(duì)象以及狀態(tài)占锯,相等返回True袒哥,否則返回False
element_located_selection_state_to_be? 傳入定位元組以及狀態(tài)缩筛,相等返回True,否則返回False
alert_is_present 是否出現(xiàn)Alert
詳細(xì)內(nèi)容:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions
前進(jìn)后退
importtimefromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.get('https://www.baidu.com/')browser.get('https://www.taobao.com/')browser.get('https://www.python.org/')browser.back()time.sleep(1)browser.forward()browser.close()
Cookies
fromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.get('https://www.zhihu.com/explore')print(browser.get_cookies())browser.add_cookie({'name':'name','domain':'www.zhihu.com','value':'germey'})print(browser.get_cookies())browser.delete_all_cookies()print(browser.get_cookies())
選項(xiàng)卡管理
importtimefromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.get('https://www.baidu.com')browser.execute_script('window.open()')print(browser.window_handles)browser.switch_to_window(browser.window_handles[1])browser.get('https://www.taobao.com')time.sleep(1)browser.switch_to_window(browser.window_handles[0])browser.get('https://python.org')
['CDwindow-4f58e3a7-7167-4587-bedf-9cd8c867f435', 'CDwindow-6e05f076-6d77-453a-a36c-32baacc447df']
異常處理
fromseleniumimportwebdriverbrowser = webdriver.Chrome()browser.get('https://www.baidu.com')browser.find_element_by_id('hello')
fromseleniumimportwebdriverfromselenium.common.exceptionsimportTimeoutException, NoSuchElementExceptionbrowser = webdriver.Chrome()try:? ? browser.get('https://www.baidu.com')exceptTimeoutException:? ? print('Time Out')try:? ? browser.find_element_by_id('hello')exceptNoSuchElementException:? ? print('No Element')finally:? ? browser.close()
No Element
詳細(xì)文檔:http://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions
作者:躺在云上數(shù)星星
鏈接:http://www.reibang.com/p/641d45a601a8
來(lái)源:簡(jiǎn)書
簡(jiǎn)書著作權(quán)歸作者所有堡称,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處瞎抛。