爬虫_selenium模拟器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39532362/article/details/87901678

导入模块

# 用于初始化
from selenium import webdriver

# 用于浏览器引擎的配置
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

# 包含获取元素element的方法
from selenium.webdriver.common.by import By

# 包含用于交互的方法
from selenium.webdriver import ActionChains

# 用于判断的等待
from selenium.webdriver.support.ui import WebDriverWait

# 超时异常
from selenium.common.exceptions import TimeoutException

初始化及配置

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

def get_driver():
  # 构造配置对象
  fp=webdriver.FirefoxProfile()

  # 用系统浏览器的配置
  #fp=webdriver.FirefoxProfile(r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\pu72dfl6.default')
  
  # 设置新窗口打开方式:1当前窗口;2新窗口;3标签页)
  fp.setPreference("browser.link.open_newwindow", 3);

  # 禁用CSS
  fp.set_preference('permissions.default.stylesheet', 2)
  
  # 禁用加载图像
  fp.set_preference('permissions.default.image', 2)
  
  # 禁用加载Flash
  fp.set_preference('dom.ipc.plugins.enabled.libflashplayer.so','false')

  # 设置文件下载路径:0下载到桌面;1下载到默认路径;2自定义下载路径;
  fp.set_preference("browser.download.folderList",2)
  fp.set_preference("browser.download.dir",os.getcwd())
  
  # 在开始下载时是否显示下载管理器
  fp.set_preference("browser.download.manager.showWhenStarting",False)
  
  # 对所给出文件类型不再弹出框进行询问
  fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")

  # 指定配置文件及启动文件路径
  return webdriver.Firefox(firefox_profile=fp,executable_path=r'.\Mozilla Firefox\geckodriver')

bw=get_driver()
bw.get(url)

设置打开窗口方式

  • 如果代码设置失败,需要修改selenium的firefox配置文件webdriver_prefs.json
  • 路径:Python\Lib\site-packages\selenium\webdriver\firefox\

手动设置

  • about:config:firefox浏览器设置地址

常用函数及属性

浏览器方法:

  • bw.get(url):提交请求
  • bw.colse():关闭浏览器
  • bw.refresh():刷新
  • bw.page_source:获取源码
  • bw.find_element_by_xpath('//*').get_attribute('outerHTML'):获取源码
  • bw.execute_script('window.scrollTo(0, document.body.scrollHeight)'):执行js
  • bw.back():后退
  • bw.forward():前进
  • bw.switch_to_window(bw.window_handles[0]):切换选项卡
  • bw.switch_to.frame(str/int)#name,id,webelement:切换到指定iframe
  • switch_to.parent_frame():切换父iframe
  • get_cookies():获取cookies
  • delete_all_cookies(): 删除所有cookies
  • add_cookies({'name':'zzz','age':18}):增加cookies

对话框:

  • bw.switch_to_alert().accept():同意对话框
  • bw.switch_to_alert().dismiss():取消对话框

元素交互:

  • ele.clear():清除可输入节点的内容
  • ele.send_key(str):在可输入节点键入内容
  • ele.click():点击节点

获取节点内容:

  • ele.id:获取id
  • ele.tag_name:获取标签名
  • ele.location:获取位置
  • ele.size:获取字节数
  • ele.text:获取文本
  • ele.get_attribute('innerText'):获取文本
  • ele.get_attribute('class'):获取属性内容

定位元素

搜索单个返回第一个元素

  • find_element_by_id(str):id
  • find_element_by_class_name(str):classname
  • find_element_by_tag_name(str):标签名
  • find_element_by_name:name
  • find_element_by_link_text(str):文字内容
  • find_element_by_partial_link_text(str):部分文字内容
  • find_element_by_css_selector(str):css选择器
  • find_element_by_xpath(str):xpath

搜索全部返回列表

  • find_elements_by_id(str):id
  • find_elements_by_class_name(str):classname
  • find_elements_by_tag_name(str):标签名
  • find_elements_by_name:name
  • find_elements_by_link_text(str):文字内容
  • find_elements_by_partial_link_text(str):部分文字内容
  • find_elements_by_css_selector(str):css选择器
  • find_elements_by_xpath(str):xpath

利用By对象定位

  • bw.find_element(By.ID, str)
  • bw.find_element(By.CLASS_NAME, str)
  • bw.find_element(By.TAG_NAME, str)
  • bw.find_element(By.NAME, str)
  • bw.find_element(By.LINK_TEXT, str)
  • bw.find_element(By.PARTIAL_LINK_TEXT, str)
  • bw.find_element(By.CSS_SELECTOR, str)
  • bw.find_element(By.XPATH, str)

交互动作

from selenium.webdriver import ActionChains

bw.switch_to.frame('iframeResult')#name,id,webelement
source=bw.find_element_by_css_selector('#draggable')
target=bw.find_element_by_css_selector('#droppable')

actions=ActionChains(bw)
actions.drag_and_drop(source,target)
actions.perform()

等待

from selenium.webdriver.support.ui import WebDriverWait

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 标题是某内容
title_contains 标题包含某内容
presence_of_element_located 元素加载出,传入定位元组,如(By.ID, 'p')
visibility_of_element_located 元素可见,传入定位元组
visibility_of 可见,传入元素对象
presence_of_all_elements_located 所有元素加载出
text_to_be_present_in_element 某个元素文本包含某文字
text_to_be_present_in_element_value 某个元素值包含某文字
frame_to_be_available_and_switch_to_it frame加载并切换
invisibility_of_element_located 元素不可见
element_to_be_clickable 元素可点击
staleness_of 判断一个元素是否仍在DOM,可判断页面是否已经刷新
element_to_be_selected 元素可选择,传元素对象
element_located_to_be_selected 元素可选择,传入定位元组
element_selection_state_to_be 传入元素对象以及状态,相等返回True,否则返回False
element_located_selection_state_to_be 传入定位元组以及状态,相等返回True,否则返

其他使用参考链接

猜你喜欢

转载自blog.csdn.net/weixin_39532362/article/details/87901678
今日推荐