# -*- 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"))

Python+Selenium webdriver Api的更多相关文章

  1. Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结

    由于网页自动化要操作浏览器以及浏览器页面元素,这里笔者就将浏览器及页面元素常用的函数及变量整理总结一下,以供读者在编写网页自动化测试时查阅. from selenium import webdrive ...

  2. Python + Selenium WebDriver Api 知识回顾

    一直再用 Selenium WebDriver 但是用的都比较零散,也没有做过总结,今天借此机会,整理一下,方便大家使用时查阅 webDriver  的属性 ['CONTEXT_CHROME', 'C ...

  3. WEB自动化(Python+selenium)的API

    在做Web自动化过程中,汇总了Python+selenium的API相关方法,给公司里的同事做了第二次培训,分享给大家                                         ...

  4. Python+Selenium+webdriver环境搭建(windows)以及相关资源下载链接

    今天记录一下测试小菜鸟alter在测试入门的一点关于python+Selenium+webdriver环境搭建的经历以及资源分享.欢迎交流学习,批评指正. 一.Python的下载与安装 1.pytho ...

  5. python selenium webdriver入门基本操作

    python selenium webdriver入门基本操作 未经作者允许,禁止转载! from selenium import webdriver import time driver=webdr ...

  6. python2.7运行selenium webdriver api报错Unable to find a matching set of capabilities

    在火狐浏览器33版本,python2.7运行selenium webdriver api报错:SessionNotCreatedException: Message: Unable to find a ...

  7. Python Selenium Webdriver常用方法总结

    Python Selenium Webdriver常用方法总结 常用方法函数 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() 最大化窗口: m ...

  8. Selenium with Python 009 - WebDriver API

    官方API文档:https://seleniumhq.github.io/selenium/docs/api/py/api.html 更多详情,最好的学习方式可以查阅官方API文档或直接阅读源码,本文 ...

  9. Selenium WebDriver Api 知识梳理

    之前一直没有系统的梳理WebDriver Api的相关知识,今天借此机会整理一下. 1.页面元素定位 1.1.8种常用定位方法 # id定位 driver.find_element_by_id() # ...

随机推荐

  1. A Very Simple Problem

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  2. HTML中动态生成内容的事件绑定问题【转载】

    转自 http://www.hitoy.org/event-binding-problem-of-dynamically-generated-content.html 由于实际的需要,有时需要往网页中 ...

  3. Nginx实现负载均衡&Nginx缓存功能

    一.Nginx是什么 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambl ...

  4. Maven的pom.xml文件详解------Build Settings

    根据POM 4.0.0 XSD,build元素概念性的划分为两个部分:BaseBuild(包含poject build和profile build的公共部分,见下)和poject buil   < ...

  5. javascript第二章--变量、作用域和内存问题

    ① 基本类型和引用类型的值 ② 执行环境及作用域 ③ 垃圾收集

  6. 插入排序-Python与PHP实现版

    插入排序Python实现 import random a=[random.randint(1,999) for x in range(0,36)] # 直接插入排序算法 def insertionSo ...

  7. 关于web变量配置问题

    关于web变量配置问题 webservice里面接口完成时考虑到接口可能用在不同的服务器,不同的数据库所以将链接地址修改成变量,在webconfig里面去修改它更加的容易方便 假始根据在同一个服务器里 ...

  8. centos6.7安装openblas错误

    centos系统:CentOS release 6.7 (Final)安装OpenBLAS # Install OpenBLAS at /usr/local/openblas git clone ht ...

  9. 移动端页面input输入框被键盘遮挡问题

    <body class="layout-fixed"> <!-- fixed定位的头部 --> <header> </header> ...

  10. 用python实现一个简单的词云

    对于在windows(Pycharm工具)里实现一个简单的词云还是经过了几步小挫折,跟大家分享下,如果遇到类似问题可以参考: 1. 导入wordcloud包时候报错,当然很明显没有安装此包. 2. 安 ...