欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python selenium

程序员文章站 2022-07-14 09:54:32
...
下载 geckodriver-v0.24.0-win64.zip 将 geckodriver.exe 放置到PATH中

pip install selenium

# test.py
from selenium import webdriver
from selenium.webdriver.support.select import Select

# 自动登录
driver = webdriver.Firefox()
driver.get("http://url")
driver.find_element_by_id('username').click()
driver.find_element_by_id('username').send_keys('username')
driver.find_element_by_id('password').click()
driver.find_element_by_id('password').send_keys('******')
Select(driver.find_element_by_id("select_id")).select_by_value("value_name")
driver.find_element_by_id('login_button').click()

# 登出
for link in driver.find_elements_by_xpath("//*[@href]"):
    if link.get_attribute('href') == 'http://logout_url':
        link.click()
        break

自动保存文件

from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.helperApps.neverAsk.saveToDisk"
                       "application/csv, application/pdf, application/octet-stream, application/vnd.ms-excel")

driver = webdriver.Firefox(firefox_options=options)

转载于:https://my.oschina.net/redhands/blog/3031543