本文主要是展示了Selecium库的基础知识和简单操作。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
browser = webdriver.Chrome() # 新建一个浏览器窗口
try:
browser.get('https://www.baidu.com') # 访问特定URL
input_info = browser.find_element_by_id('kw') # 找到为 kw 的 id
input_info.send_keys('Python') # 输入为 Python 的关键词
input_info.send_keys(Keys.ENTER) # 点击 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() # 最后,关闭浏览器。
from selenium import webdriver
browser = webdriver.Chrome() # 指定特定的浏览器操作。
browser = webdriver.Firefox()
browser = webdriver.Edge()
browser = webdriver.PhantomJS()
browser = webdriver.Safari()
from selenium import webdriver
brower = webdriver.Chrome()
brower.get('heeps://www.taobao.com')
print(brower.page_source)
brower.close()
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
# 使用不同的方式进行查找同一个内容 id css_selector xpath
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('heeps://www.taobao.com')
input_first = browser.find_element(By.ID, 'q')
print(input_first)
browser.close()
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
lis = browser.find_elements_by_css_selector('.service-bd li')
print(lis)
browser.close()
# 类似于上面的这种方法
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
lis = browser.find_elements(By.CSS_SELECTOR, '.service-bd li')
print(lis)
browser.close()
对获取的元素调用交互方法
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
inpit_info = browser.find_element_by_id('q')
input_info.send_keys('iphone')
time.slees(1)
input_info.clear()
input_info.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
将动作附加到动作链中串行执行
from selenium import webdriver
from selenium.webdriver import ActionChains
browser = webdriver.Chrome()
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser.get(url)
browser.switch_to.frame('iframeResult') # 传入frame参数
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
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
# 进度条的拖拽
browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
browser.execute_script('alert("To Bottom")') # 拖拽到底部。
from selenium import webdriver
from selenium.webdriver import ActionChains
browser = 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'))
from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
input_info = browser.find_element_by_class_name('Sticky AppHeader')
print(input_info.text)
from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
input_info = browser.find_element_by_class_name('zu-top-add-question')
print(input_info.id)
print(input_info.location)
print(input_info.tag_name)
print(input_info.size)
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
browser = 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')
except NoSuchElementException:
print('NO LOGO')
browser.switch_to.parent_frame()
logo = browser.find_element_by_class_name('logo')
print(logo)
print(logo.text)
当使用了隐式等待执行测试点 时候,如果 WebDriver 没有在 DOM 中找到元素,将继续等待,超出设定时间后则跑出找不到元素的异常。换句话说,当查找元素或者元素并没有立即出现的时候,饮食等待将等待一段时间再查找 DOM,默认时间为0.
from selenium import webdriver
browser = 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)
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
browser = webdriver.Chrome()
browser.get('https://www.taobao.com/')
wait = WebDriverWait(browser, 10)
input_info = 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_info, button)
详细内容:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com/')
browser.get('https://www.taobao.com/')
browser.get('https://www.python.org/')
browser.back()
time.sleep(2)
browser.forward()
browser.close()
from selenium import webdriver
browser = 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())
browser.close()
import time
from selenium import webdriver
browser = 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')
time.sleep(3)
browser.close()
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.find_element_by_id('hello') # id 不存在
# +++++++++++++++++++++++++++++++++++++++++++++++++ #
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
browser = webdriver.Chrome()
try:
browser.get('https://www.baidu.com')
except TimeoutException:
print('Time Out!')
try:
browser.find_element_by_id('hello')
except NoSuchElementException:
print('No Element')
finally:
browser.close()
详细文档:http://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions
今天主要是学习了Selenium的基础知识和简单使用,对于具体的实践,还是要多实战来看。对该库有兴趣的小伙伴,可以观看官方文档。
溜了遛了,脑壳疼。Loading(34/100)。。。