標籤:檔案 pen main sele driver selenium pytho open ali
#練習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.txt10 11 from selenium import webdriver12 import sys13 import time14 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_url37 driver.quit()
【Python】數字驅動