Selenium库的基本使用
1.基本使用
from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitbrower = webdriver.Chrome()try: brower.get('https://www.baidu.com') input = brower.find_element_by_id('kw')#查找id为kw的元素 input.send_keys('Python')#向input文本框输入python input.send_keys(Keys.ENTER)#输入回车 wait = WebDriverWait(brower,10)#等待浏览器加载 wait.until(EC.presence_of_element_located((By.ID,'content_left')))#查找id为content——left的对象 print(brower.current_url)#输出当前的url print(brower.get_cookies())#输出当前的cookies print(brower.page_source)#输出当前的网页源码finally: brower.close()
2.声明浏览器对象
from selenium import webdriverbrower = webdriver.Chrome()brower = webdriver.Firefox()brower = webdriver.Edge()brower = webdriver.PhanromJS()brower = webdriver.Safari()
3.访问页面
from selenium import webdriverbrower = webdriver.Chrome()brower.get('https://ww.taobao.com')print(brower.page_source)brower.colse()
4.查找元素
单个元素
from selenium import webdriverbrowser = 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()
结果:
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get('https://www.taobao.com') input_first=browser.find_element(By.ID,'q') print(input_first) browser.close() 结果:
多个元素
from selenium import webdriverbrowser = webdriver.Chrome()browser.get('https://www.taobao.com')list=browser.find_elements_by_css_selector('.service-bd li')print(list)browser.close()from selenium import webdriverbrowser = webdriver.Chrome()browser.get('https://www.taobao.com')list=browser.find_elements(By.CSS_SELECTOR,'.service-bd li')print(list)browser.close()
5.元素交互操作
from selenium import webdriverimport timebrowser = 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()
6.交互动作
from selenium import webdriverfrom selenium.webdriver import ActionChainsbrower=webdriver.Chrome()url="https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"brower.get(url)brower.switch_to.frame('iframeResult')source=brower.find_element_by_css_selector('#draggable')target=brower.find_element_by_css_selector('#droppable')action=ActionChains(brower)action.drag_and_drop(source,target)action.perform()
7.执行JavaScript
from selenium import webdriverbrower = webdriver.Chrome()brower.get('https://www.zhihu.com/explore')brower.execute_script('window.scrollTo(0,document.body.scrollHeight)')brower.execute_script('alert("To Bottom")')
8.获取属性
from selenium import webdriverfrom selenium.webdriver import ActionChainsbrower = webdriver.Chrome()url='https://www.zhihu.com/explore'brower.get(url)logo = brower.find_element_by_id('zh-top-link-logo')print(logo)print(logo.get_attribute('class')) 获取:
zu-top-link-logo
9.获取文本值
from selenium import webdriverbrower = webdriver.Chrome()url='https://www.zhihu.com/explore'brower.get(url)input=brower.find_element_by_class_name('zu-top-add-question')print(input.text) 结果:
提问
10.获取ID,位置,标签名,大小
from selenium import webdriverbrower = webdriver.Chrome()url='https://www.zhihu.com/explore'brower.get(url)input=brower.find_element_by_class_name('zu-top-add-question')print(input.id)print(input.location)print(input.tag_name)print(input.size) 结果:
0.5790966028649505-1{'x': 760, 'y': 7}button{'height': 32, 'width': 66}
11.Frame
import timefrom selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionbrower = webdriver.Chrome()url="https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"brower.get(url)brower.switch_to.frame('iframeResult')source=brower.find_element_by_css_selector('#draggable')print(source)try: logo=brower.find_element_by_class_name('logo')except NoSuchElementException: print('No Logo')brower.switch_to.parent_frame()logo=brower.find_element_by_class_name('logo')print(logo)print(logo.text)结果:No Logo RUNOOB.COM
12.隐式等待
from selenium import webdriverbrower = webdriver.Chrome()brower.get('https://www.zhihu.com/explore')brower.implicitly_wait(10)input=brower.find_element_by_class_name('zu-top-add-question')print(input)结果:
13.显示等待
from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitbrower = webdriver.Chrome()brower.get('https://www.taobao.com')wait=WebDriverWait(brower,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)结果:
14.前进后退
from selenium import webdriverimport timebrower = webdriver.Chrome()brower.get('https://www.baidu.com')brower.get('https://www.taobao.com')brower.get('https://www.python.org')brower.back()time.sleep(1)brower.forward()brower.close()
15.Cookies
from selenium import webdriverbrower = webdriver.Chrome()brower.get('https://www.zhihu.com/explore')print(brower.get_cookies())brower.add_cookie({ 'name':'name','domain':'www.zhihu.com','value':'germey'})print(brower.get_cookies())brower.delete_all_cookies()print(brower.get_cookies())
16.选项卡管理
import timefrom selenium import webdriverbrower = webdriver.Chrome()brower.get('https://www.baidu.com')brower.execute_script('window.open()')print(brower.window_handles)brower.switch_to_window(brower.window_handles[1])brower.get('https://www.taobao.com')time.sleep(1)brower.switch_to_window(brower.window_handles[0])brower.get('https://www.python.org') 结果:
['CDwindow-4971CB8E7903DFAE9CBA82A6A5F05DB2', 'CDwindow-652D8829B41E67E7F7E8C2E146F8C353']
17.异常处理
from selenium import webdriverbrower=webdriver.Chrome()brower.get('https://www.baidu.com')brower.find_element_by_id('hello')
from selenium import webdriverfrom selenium.common.exceptions import TimeoutException,NoSuchElementExceptionbrower=webdriver.Chrome()try: brower.get('https://www.baidu.com')except TimeoutException: print('Time Out')try: brower.find_element_by_id('hello')except NoSuchElementException: print('No Element')finally: brower.close()结果:No Element