1,下載瀏覽器chrome 2,驅動下載:http://npm.taobao.org/mirrors/chromedriver/ 對應版本下載後放入path from selenium import webdriver from selenium.webdriver.chrome.options i ...
1,下載瀏覽器chrome
2,驅動下載:http://npm.taobao.org/mirrors/chromedriver/ 對應版本下載後放入path
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time,os
chrome_opt = Options() # 創建參數設置對象.
chrome_opt.add_argument('--no-sandbox') # 允許root
chrome_opt.add_argument('--headless') # 無界面化.
chrome_opt.add_argument('--disable-gpu') # 配合上面的無界面化.
chrome_opt.add_argument('--window-size=1366,768') # 設置視窗大小, 視窗大小會有影響.
driver = webdriver.Chrome(chrome_options=chrome_opt) # 創建Chrome對象並傳入設置信息.
driver.get('https://www.baidu.com') # get方式訪問百度.
name = driver.find_element_by_name('uid')
passwd = driver.find_element_by_name('upw')
submit = driver.find_element_by_name('smbtn')
driver.execute_script("arguments[0].value = 'name';", name)
driver.execute_script("arguments[0].value = 'passwd';", passwd)
submit.click()
iframe = driver.find_element_by_name('zzj_top_6s')
src = iframe.get_attribute('src')
driver.get(src)
submit2 = driver.find_element_by_xpath('//*[@id="bak_0"]/div[13]/div[3]/div[4]')
submit2.click()
submit3 = driver.find_element_by_xpath('/html/body/form/div[3]/div[4]')
submit3.click()
print(driver.page_source) # 列印載入的page code, 證明(prove) program is right.
driver.quit() # 使用完, 記得關閉瀏覽器, 不然chromedriver.exe進程為一直在記憶體中.