# selenium:自动化测试工具,支持多种浏览器,解决爬虫中js渲染问题
# 基本使用
# 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')
# input = browser.find_element_by_id('kw') # 搜索id为kw的元素
# input.send_keys('python') # 输入python
# input.send_keys(Keys.ENTER) # 敲击enter
# wait = WebDriverWait(browser,10) # 设置浏览器等待时间
# wait.until(EC.presence_of_element_located((By.ID,'content_left'))) # 等待id为content加载出来
# print(browser.current_url) # 打印出url
# print(browser.get_cookies()) # 打印出cookies
# print(browser.page_source) # 打印出源代码
# finally:
# browser.close()
#
# 访问网页
# from selenium import webdriver
# browser = webdriver.Chrome()
# browser.get('https:www.taobao.com') # 访问淘宝网址
# print(browser.page_source) # 打印源代码
# # browser.close() # 关闭浏览器
# from selenium import webdriver
# browser = webdriver.Chrome()
# browser.get('https://www.taobao.com')
# first = browser.find_element_by_id('q') #找id为q的三种方法,效果相同
# second = browser.find_elements_by_css_selector('#q')
# third = browser.find_elements_by_xpath('//*[@id="q"]')
# print(first,second,third)
# browser.close()
# # 通用查找方式
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# browser = webdriver.Chrome()
# browser.get('https://www.taobao.com')
# first = browser.find_element(By.ID,'q')
# print(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()
# 元素的交互操作
# from selenium import webdriver
# import time
# browser = webdriver.Chrome()
# browser.get('https://www.taobao.com')
# input = browser.find_elements_by_id('q') # 找到搜索框的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()
# browser.close()
# 获取信息
#获取属性值
# 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')) # 查找属性值
# 获取文本值,ID,位置,标签名,大小
# from selenium import webdriver
# browser = webdriver.Chrome()
# browser.get('https://www.zhihu.com/explore')
# input = browser.find_element_by_class_name('zu-top-add-question') # 找到id的name,将名字传入参数当中
# print(input.text) # 打印出文本内容
# print(input.id) # 获取id
# print(input.location)
# print(input.tag_name)
# print(input.size)
# Frame
#
# import time
# from selenium import webdriver
# from selenium.common.exceptions import NoSuchElementException
# browser = webdriver.Chrome()
# browser.get('http://www.runoob.com/try/try.php?filename=jquery-api-droppable')
# browser.switch_to_frame() # 该方法在python3.6版本无法使用,跳过
# 等待
# 当查找元素没有立即出现的时候,需要设置等待时间再次查找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)
# 前进后退
# 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(1)
# browser.forward()
# browser.close()
# cookies
# from selenium import webdriver
# browser = webdriver.Chrome()
# print(browser.get_cookies())
# browser.add_cookie({'name':'name','domian':'www.zhihu.com','value':'ger'})
# print(browser.get_cookies())
# browser.delete_all_cookies()
# print(browser.get_cookies())
#
python爬虫之selenium库
最新推荐文章于 2020-05-25 13:13:13 发布