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目录下

  • 相关阅读:
    kafka整理笔记笔记
    node(一)安装nodejs最新版到debian,ubuntu,mint系统
    ubuntu16.04安装visual-studio-code
    ubuntu16.04更新内核--使用4.6以上的内核会让用A卡的Dell电脑更快--及卸载多余内核
    linux查看主板型号及内存硬件信息,及硬盘测速
    git使用,在ubuntu中
    Ubuntu中文目录文件夹改为英文
    w3m 在ubuntu中的使用
    关于右键属性与du -sh显示的文件大小不一致的解决
    ubuntu16.04安装chrome
  • 原文地址:https://www.cnblogs.com/bincoding/p/6783598.html
Copyright © 2011-2022 走看看