selenium库的学习

selenium库使用步骤

  1. 导入相关库,创建webdriver对象
  2. 访问页面
  3. 创建延迟等待WebDriverWait对象
  4. 返回HTML代码
  5. 找相关节点是为了模仿点击行为 单纯找节点利用其他库
常见应用:模仿下拉进度条加载数据

常见对象和方法

  1. browser = webdriver.Chrome() <class 'selenium.webdriver.chrome.webdriver.WebDriver'

  2. find_element() <class 'selenium.webdriver.remote.webelement.WebElement'>

  3. wait = WebDriverWait(browser, 10) <class 'selenium.webdriver.support.wait.WebDriverWait'>

导入库准备

from selenium import webdriver
from selenium.webdriver.common.by import By #class By类中包含find_element中by的方式
from selenium.webdriver.common.keys import Keys #class Set of special keys codes.
from selenium.webdriver.support import expected_conditions as EC #wait对象中until函数的等待条件
from selenium.webdriver.support.wait import WebDriverWait #class

常用操作练习

from selenium import webdriver
from selenium.webdriver.common.by import By #class
from selenium.webdriver.common.keys import Keys #class
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #class
import time
 
browser = webdriver.Chrome()
#print(type(browser))
#<class 'selenium.webdriver.chrome.webdriver.WebDriver'>
try:
    #访问页面
    browser.get('https://www.taobao.com')
    #<class 'NoneType'>

    #选择单个节点
    #-----------------------------------
    """
    find_element_by_id
    find_element_by_name
    find_element_by_xpath
    find_element_by_link_text
    find_element_by_partial_link_text
    find_element_by_tag_name
    find_element_by_class_name
    find_element_by_css_selector
    find_element(By.ID, 'q')  查找方式By和值
    """
    #-----------------------------------
    #input_first = browser.find_element_by_id('q')
    #input_second = browser.find_element_by_css_selector('#q')
    input=browser.find_element_by_xpath(r'//input[contains(@type,"text") and @name="keyword"]')
    #<class 'selenium.webdriver.remote.webelement.WebElement'>

    #关键字搜索
    input.send_keys('龙虾')   #<class 'NoneType'>
    input.send_keys(Keys.ENTER)
    #input.clear() #清理搜索框

    #按钮点击,现在淘宝不行了需要登录
    button = browser.find_element_by_class_name('btn-search')
    button.click()
    
    #显式等待,创建wait对象
    wait = WebDriverWait(browser, 10)
    #<class 'selenium.webdriver.support.wait.WebDriverWait'>
    wait.until(EC.presence_of_element_located((By.ID, 'content_left')))
    #button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))
    print(browser.current_url)
    print(browser.get_cookies())

    #查找多个节点,返回结果为列表类型
    lis = browser.find_elements_by_css_selector('.service-bd li')
    #--------------------------------------
    """
    find_elements_by_id
    find_elements_by_name
    find_elements_by_xpath
    find_elements_by_link_text
    find_elements_by_partial_link_text
    find_elements_by_tag_name
    find_elements_by_class_name
    find_elements_by_css_selector
    find_elements(By.CSS_SELECTOR, '.service-bd li')
    """
    #--------------------------------------

    #下拉进度条
    from selenium import webdriver
    browser = webdriver.Chrome()
    browser.get('http://www.xiachufang.com/category/2918/')
    browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
    browser.execute_script('alert("To Bottom")')

    #前进和后退
    browser.back()
    time.sleep(1)
    browser.forward()
    #切换frame

    #cookies
    browser.get_cookies()
    browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey'})
    browser.get_cookies()
    browser.delete_all_cookies()

finally:
    time.sleep(10)
    browser.close()

posted on 2020-03-31 01:02  一切为了生活  阅读(156)  评论(0编辑  收藏  举报