#练习1:打开3个网址,每个等3秒钟

urls.txt:

http://www.baidu.com
http://www.sogou.com
http://www.sohu.com

main.py:
from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path = "c:\\chromedriver")
with open("urls.txt") as fp: #urls.txt里存三个网址
for url in fp:
driver.get(url)
time.sleep(3)
driver.current_url
driver.quit()

 1 #练习2:通过命令行选择浏览器或文件打开和执行
 2 urls.txt:
 3 http://www.baidu.com
 4 http://www.sogou.com
 5 http://www.sohu.com
 6 
 7 main.py:
 8 #python test.py chrome  http://www.sohu.com
 9 #python test.py ie  urls.txt
10 
11 from selenium import webdriver
12 import sys
13 import time
14 
15 if len(sys.argv)!=3:
16     print "parameter number is not valid!"
17     sys.exit()
18 
19 browser_type=sys.argv[1]
20 file_or_url=sys.argv[2]
21 
22 if browser_type.lower()=="chrome":
23     driver = webdriver.Chrome(executable_path = "c:\\chromedriver")
24 elif browser_type.lower()=="ie":
25     driver = webdriver.Ie(executable_path = "c:\\IEDriverServer")
26 else:
27     driver = webdriver.Firefox(executable_path = "c:\\geckodriver")
28 
29 if file_or_url.find("http://")!=-1:
30     driver.get(file_or_url)
31 else:
32     with open(path) as fp: #urls.txt里存三个网址
33         for url in fp:
34             driver.get(url)
35             time.sleep(3)
36             driver.current_url
37 driver.quit()