Selenium

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进程为一直在内存中.

你可能感兴趣的:(Selenium)