zoukankan      html  css  js  c++  java
  • selenium,webdriver模仿浏览器访问百度 基础2

    学python理念  :  代码要多敲 一定要多敲 哪怕很基础  注释要清晰

    由于基础1有一些注释写的很详细,

    在这里有些注释没有写的很详细

    可以配合基础1一起学习哦

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time,os
    from lxml import etree
    
    # base_url = 'http://www.baidu.com/'
    
    #创建浏览器对象
    driver = webdriver.PhantomJS()
    #get方法 打开网页
    driver.get('http://www.baidu.com/')
    
    #获取标题title
    print(driver.title)#百度一下,你就知道
    
    #获取页面中标签的内容
    #创建文件夹
    root_dir = 'bai_du'
    if not os.path.exists(root_dir):
        os.mkdir(root_dir)
    
    #截屏保存
    file_name = root_dir + '/%s.png'%(driver.title)
    driver.save_screenshot(file_name)
    
    #输入框
    obj = driver.find_element_by_id('kw')
    obj.send_keys('詹姆斯')
    # time.sleep(4)
    # driver.save_screenshot(file_name)
    
    button = driver.find_element_by_id('su')
    button.click()
    time.sleep(4)
    driver.save_screenshot(file_name)
    
    #cookie 获取百度缓存的cookies 组成列表
    cookies = driver.get_cookies()
    for key in cookies:
        print(key)
    
    #下面书有趣内容 获取按键 基本上每一个键盘上的按键都可以获取
    #全选操作 相当于键盘同时按下control+a
    obj.send_keys(Keys.CONTROL,'a')
    driver.save_screenshot('bai_du/homepage_control_a.png')
    
    #剪切操作
    obj.send_keys(Keys.CONTROL,'x')
    driver.save_screenshot('bai_du/homepage_control_x.png')
    
    #粘贴操作 页面的输入框出现三个詹姆斯
    obj.send_keys(Keys.CONTROL,'v')
    obj.send_keys(Keys.CONTROL,'v')
    obj.send_keys(Keys.CONTROL,'v')
    driver.save_screenshot('bai_du/homepage_control_v.png')
    
    #获取当前的url
    print(driver.current_url)
    
    #关闭页面
    driver.close()
    
    #关闭浏览器
    driver.quit()
  • 相关阅读:
    Timer类的常见使用方法
    转:HTTP协议简介
    DS-5新加交叉编译工具
    C++的引用
    C++构造函数的几种使用方法
    Eclipse 快捷键
    eclipse Outline里图标的含义
    Linux 内存映射函数 mmap()函数详解
    Sockit 硬件接口编程——点亮一个LED
    更新Preloader与uboot
  • 原文地址:https://www.cnblogs.com/zhangboblogs/p/8562915.html
Copyright © 2011-2022 走看看