zoukankan      html  css  js  c++  java
  • python使用selenium

    首先安装 

    pip install selenium

    测试抓取baidu,其中的chromedriver.exe需要自己下载,百度有很多的

    import time
    from
    selenium import webdriver browser = webdriver.Chrome('C:Program Files (x86)GoogleChromeDriverchromedriver.exe') # 可选参数,如果不指定将搜索环境变量 browser.get('http://www.baidu.com/')
    time.sleep(5)

     执行结果如下

    进阶

    import time
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome('C:Program Files (x86)GoogleChromeDriverchromedriver.exe')
    driver.get('https://www.baidu.com/')
    elem = driver.find_element_by_id('kw') 
    elem.send_keys("test")
    elem.send_keys(Keys.RETURN)
    print(driver.page_source)
    
    #<input type="text" name="passwd" id="passwd-id" />
    
    element = driver.find_element_by_id("passwd-id")
    element = driver.find_element_by_name("passwd")
    element = driver.find_elements_by_tag_name("input")
    element = driver.find_element_by_xpath("//input[@id='passwd-id']")

     如果遇到错误

    C:UsersinAppDataLocalProgramsPythonPython36python.exe C:/Users/bin/PycharmProjects/Test/Test/Crawler/TestSelenium.py
    Traceback (most recent call last):
      File "C:UsersinAppDataLocalProgramsPythonPython36libsite-packagesseleniumwebdrivercommonservice.py", line 74, in start
        stdout=self.log_file, stderr=self.log_file)
      File "C:UsersinAppDataLocalProgramsPythonPython36libsubprocess.py", line 707, in __init__
        restore_signals, start_new_session)
      File "C:UsersinAppDataLocalProgramsPythonPython36libsubprocess.py", line 990, in _execute_child
        startupinfo)
    FileNotFoundError: [WinError 2] 系统找不到指定的文件。
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:/Users/bin/PycharmProjects/Test/Test/Crawler/TestSelenium.py", line 3, in <module>
        browser = webdriver.Chrome()
      File "C:UsersinAppDataLocalProgramsPythonPython36libsite-packagesseleniumwebdriverchromewebdriver.py", line 62, in __init__
        self.service.start()
      File "C:UsersinAppDataLocalProgramsPythonPython36libsite-packagesseleniumwebdrivercommonservice.py", line 81, in start
        os.path.basename(self.path), self.start_error_message)
    selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

    原因是没有安装chrome driver,到 https://sites.google.com/a/chromium.org/chromedriver/downloads 下载,

    如果是windows就将chrome driver添加到Path环境变量中,Linux直接放到 usr/bin目录下

  • 相关阅读:
    Vue 之 mixin的用法
    react 初学之 jsx ,prop。state
    前端清除缓存的集中方法
    关于event被废弃后的新实用方法
    JS获取字符串的字节长度
    getPopupContainer解决ant-design-vue select组件下拉框偏移错位
    frameset标签使用
    浏览器内核
    两种多关键字排序代码
    两种多关键字排序策略比较
  • 原文地址:https://www.cnblogs.com/bincoding/p/6783598.html
Copyright © 2011-2022 走看看