• Python+Selenium webdriver Api


    # -*- coding: utf-8 -*-
    from selenium import webdriver
    
    browser = webdriver.Firefox()
    
    #browser.set_window_size(480,800)              #设置浏览器窗口大小
    # browser.find_element_by_id("kw").send_keys("陈月白")  #通过id定位元素,并对此元素使用send_keys()方法传入值
    # browser.find_element_by_id("su").click()          #对所定位的元素使用click()方法点击
    
    browser.maximize_window()       #将浏览器最大化
    first_url = "https://www.baidu.com/"
    
    print("now access %s"%first_url)
    browser.get(first_url)      #对browser对象使用get()方法访问url
    
    second_url = "http://news.baidu.com/"
    
    print("now access %s"%second_url)
    browser.get(second_url)
    
    #后退到百度首页(即后退至上一个页面)
    print("back to %s"%first_url)
    browser.back()                    #对browser对象使用back()方法后退
    
    #前进到新闻页面
    print("forward to %s"%second_url)
    browser.forward()                #对browser对象使用forward()方法前进
    
    #browser.quit()
     
    例子:
    # -*- coding: utf-8 -*-
    
    from selenium import webdriver
    import time
    
    ###########################################
    #完成一个小功能,控制打开
    #fireFox--百度首页--搜索月白--进入该词条百度百科
    ###########################################
    
    #
    def _heading(s):
        flag = "="
        return "%s\n"%(len(s)*flag) + "%s\n"%s + len(s)*flag
    
    url = "https://www.baidu.com/"
    searchKey = "月白"
    
    browser = webdriver.Firefox()
    browser.maximize_window()
    
    browser.get(url)      #访问百度首页
    time.sleep(2)
    
    browser.find_element_by_id("kw").send_keys(searchKey)       #根据id找到元素并操作
    browser.find_element_by_id("su").click()
    print("access to baidu success,title =",browser.title)
    
    print("begin to access %s 百度百科"%searchKey)
    browser.find_element_by_xpath(u".//*[@id='1']/h3/a").click()
    time.sleep(2)
    print("access to  %s 百度百科 success!"%searchKey)
    time.sleep(5)
    
    browser.quit()
    print(_heading("run script success"))


  • 相关阅读:
    CAS在tomcat6.0.18下的SSO
    CAS在tomcat6.0.18下的SSO
    PL/SQL 0.几秒出结果,SQL效率一定高吗?
    优化大型复杂SQL
    C++ ProtoBuf小结
    protobuf c++入门
    Oracle_inner join,left join,right join,full join 的区别,用例子说明
    Linux_查看修改SWAP大小
    Oracle 优化器
    Oracle 行转列两种方法
  • 原文地址:https://www.cnblogs.com/chenyuebai/p/5384715.html
Copyright © 2020-2023  润新知