<
模块方法: 含义:
<<定位元素>>
定位一个元素: 定位多个元素: 含义:
<<等待条件函数>>
等待条件: 含义:
<<Chrome浏览器配置选项>>
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
chrome_options.add_experimental_option(‘excludeSwitches‘, [‘enable-automation‘])
chrome_options.add_argument(‘--headless‘)
options.add_argument(‘lang=zh_CN.UTF-8‘)
<模拟 Android QQ浏览器>
options.add_argument(‘user-agent="MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"‘)
<模拟iPhone 6>
options.add_argument(‘user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"‘)
browser.set_window_size(configure.windowHeight, configure.windowWidth) ## configure为桌面分辨率实际大小
prefs = {}
prefs[“credentials_enable_service”] = False
prefs[“profile.password_manager_enabled”] = False
options.add_experimental_option(“prefs”, prefs)
extension_path = ‘D:/extension/XPath-Helper_v2.0.2.crx‘
chrome_options.add_extension(extension_path)
self.driver = webdriver.Chrome(options=chrome_options)
import time
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
class Crawler(object):
def init(self, url=‘‘):
Chrome常用配置选项:
service_args = [‘--load-images=false‘, ‘--disk-cache=true‘]
self.driver = webdriver.PhantomJS(service_args=service_args)
"""
self.source_url = url
self.driver = webdriver.Chrome() ## 选择浏览器驱动
self.waiting = WebDriverWait(self.driver, 30) ## 设置显示等待30秒
self.driver.implicitly_wait(30) ## 设置隐示等待30秒
self.actions = webdriver.ActionChains(self.driver) ## 动作链初始化
def switch_to_windows(self, to_parent_windows=False):
"""
切换到不同的windows窗口
:param to_parent_windows: 默认为False,如果设置为True则回到主窗口
:return:
"""
total = self.driver.window_handles
if to_parent_windows:
self.driver.switch_to.window(total[0])
else:
current_windows = self.driver.current_window_handle
for window in total:
if window != current_windows:
self.driver.switch_to.window(window)
def switch_to_frame(self, index=0, to_parent_frame=False, to_default_frame=False):
"""
切换到不同的frame框架
:param index: expect by frame index value or id or name or element
:param to_parent_frame: 默认为False,如果设置为True则切换到上一个frame框架
:param to_default_frame: 默认为False,如果设置为True则切换到最上层的frame框架
:return:
"""
if to_parent_frame:
self.driver.switch_to.parent_frame()
elif to_default_frame:
self.driver.switch_to.default_content()
else:
self.driver.switch_to.frame(index)
def open_new_windows(self, new_url=‘‘):
"""
打开一个新的windows窗口
:param new_url: 新的URL
:return:
"""
js = "window.open({})".format(new_url)
self.driver.execute_script(js)
time.sleep(2)
def page_scrolling(self, go_to_bottom=False, rolling_distance=(0, 1000)):
"""
页面滚动,如果没有滚动效果,添加延时(页面需要全部加载完毕才能滚动)
:param bool go_to_bottom: 默认为False,如果为True则滚动到当前页面的最底部
:param tuple rolling_distance: 滚动距离,默认是向下滚动1000像素
:return:
"""
time.sleep(5)
if go_to_bottom:
js = "window.scrollTo(0, document.body.scrollHeight)"
else:
js = "window.scrollBy({}, {})".format(rolling_distance[0], rolling_distance[1])
self.driver.execute_script(js)
def screen_shot(self, picture_name=‘example.jpg‘):
"""
截取当前网页并保存为图片
:param picture_name: 保存的图片名称
:return:
"""
self.driver.save_screenshot(picture_name)
def action_chain(self, source, target):
"""
执行鼠标拖曳
:param source: 拖曳前位置
:param target: 拖曳后位置
:return:
"""
self.actions.drag_and_drop(source, target)
self.actions.perform()
def close_current_windows(self):
if self.driver:
self.driver.close()
def quit_browser(self):
if self.driver:
self.driver.quit()
def main(self):
self.driver.get(self.source_url)
self.driver.find_element_by_xpath(‘//[@id="kw"]‘) ## 通过xpath定位
input_box = self.waiting.until(EC.presence_of_element_located((By.XPATH, ‘//[@id="kw"]‘))) ## 通过等待条件定位
print(input_box.get_attribute(‘class‘)) ## 获取节点的class属性值
print(input_box.id) ## 获取节点的id值
print(input_box.text) ## 获取节点的文本值
print(input_box.location) ## 获取节点在页面中的相对位置
print(input_box.tag_name) ## 获取节点的标签名称
print(input_box.size) ## 获取节点的大小
print(self.driver.current_url) ## 获取当前的URL
print(self.driver.get_cookies()) ## 获取当前的Cookies
print(self.driver.page_source) ## 获取网页源代码
input_box.clear() ## 清空文本
input_box.send_keys(‘python‘) ## 输入文本
input_box.send_keys(Keys.ENTER) ## 执行输入
self.driver.back() ## 网页后退
time.sleep(1)
self.driver.forward() ## 网页前进
self.page_scrolling() ## 执行javascript
source = self.driver.find_element_by_xpath(‘//[@id="result_logo"]/img[1]‘)
target = self.driver.find_element_by_xpath(‘//[@id="kw"]‘)
self.action_chain(source=source, target=target) ## 鼠标拖曳
if name == ‘main‘:
crawler = Crawler(url=‘https://www.baidu.com‘)
crawler.main()
原文:https://www.cnblogs.com/gqv2009/p/12218260.html