Python Program Example 1.

If you’re going to need a refresh on the same tab, you’ll need selenium webdriver. After installing selenium using pip, you can use the following code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get("http://www.python.org")
while True:
   time.sleep(10)
   driver.refresh()

If you are browsing a static page, you can pass a parameter in it and run, for example passing "pycon" to search as below script:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

Python Program Example 2.

from link http://geekinessthecoolway.blogspot.hk/2013/05/tired-of-refreshingscript-for-automatic.html

A script will automatically refresh the page after every few seconds,so that my keyboard’s F5 button is spared. But still there is redundancy, one has to keep looking at the same thing again and again to check if any change has happened. So,I added few more lines to the script. Now whenever the result will be declared (or there will be any new notification) a song will start playing automatically.

Here is the resultant script:

import urllib
import time
import os
import pygame
uri = “http://upresults.nic.in”  #url where result will be declared
source = urllib.urlopen(uri).read()
nw_source=source
cntr=0
flg=True
while nw_source==source:
if flg:
time.sleep(5)  #refresh every 5 seconds
try:
nw_source = urllib.urlopen(uri).read()
except IOError:
print “Error in reading url”
flg=False
continue
cntr+=1
print cntr,” times refreshed”

flg=True
pygame.init()
pygame.mixer.music.load(“kar_chale _hum_vida.mp3”) #pass the path to the music file
pygame.mixer.music.play()
while True:
pass

Using Webdriver under Selenium:

WebDriver是主流Web应用自动化测试框架,具有清晰面向对象 API,能以最佳的方式与浏览器进行交互。

支持的浏览器:

  • Mozilla Firefox
  • Google Chrome
  • Microsoft Internet Explorer
  • Opera
  • Safari
  • Apple iPhone
  • Android browsers

Selenium WebDriver 又称为 Selenium2。

Selenium 1 + WebDriver = Selenium 2

标准的安装步骤

  1. 选择Python的版本。Python主流的有两个大的版本,2.7和3.5(请注意,从Python的3.5版本开始,不再支持Windows XP操作系统,Windows XP用户请安装3.4版本)。我们的例子将会选用面向未来的3.5版本。
  2. 在Windows安装Selenium2.0,有两种途径。使用pip命令行或者源码安装。以下两种方法,使用任何一个均可。推荐pip的方式。
    1. 方法一:pip命令行安装,运行 | cmd,打开命令行,-U其实就是--upgrade,升级安装。
      pip install -U selenium
    2. 方法二:源码解压安装,前往https://pypi.python.org/pypi/selenium下载最新版的PyPI版本的Selenium,解压后执行
      python setup.py install

Source Information: http://www.jianshu.com/p/3ce95cbc65be

Selenium 3.0.1 出现的问题以及解决

3.0.1 更新以后,需要做两个操作:

  1. Geckodriver executable needs to be in PATH。Geckodirver的下载地址:https://github.com/mozilla/geckodriver/releases
    报错内容:

    WebDriverException:Message:'geckodriver'executable needs to be in Path

    geckodriver是一原生态的第三方浏览器,对于selenium3.x版本都会使用geckodriver来驱动firefox,所以需要下载geckodriver.exe。放置在Path 环境变量可以访问到的地方。例如 C:python34

  2. 需要将火狐的安装路径放到path,然后重启(必须重启电脑)
    报错内容:

    selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

    参考地址:http://stackoverflow.com/questions/40208051/selenium-using-python-geckodriver-executable-needs-to-be-in-path/40208762

Leave a Reply

Your email address will not be published.

Enter Captcha Here : *

Reload Image