我试图弄清楚为什么我的代码在无头模式下没有得到下载文件,而在常规/头模式下运行时,我确实得到了下载文件。直到本周,它一直运行得很好。
这个脚本打开一个公共的谷歌驱动器,然后在选择第一个下载链接之前应用列表视图和排序方向。以下是我从inter-web上键入并复制/粘贴的内容:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--proxy-server='direct://'")
chrome_options.add_argument("--proxy-bypass-list=*")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--ignore-certificate-errors')
prefs = {}
prefs["profile.default_content_settings.popups"]=0
prefs["download.default_directory"]=download_path
chrome_options.add_experimental_option("prefs", prefs)
browser = splinter.Browser('chrome',options=chrome_options)
browser.visit('https://drive.google.com/drive/folders/')
logging.debug("Google Drive website opened")
time.sleep(5)
WebDriverWait(browser.driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="List view"]'))).send_keys(keys.Keys.ENTER)
logging.debug("List view requested")
time.sleep(5)
WebDriverWait(browser.driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Reverse sort direction"]'))).send_keys(keys.Keys.ENTER)
logging.debug("Sort with latest on top requested")
time.sleep(5)
WebDriverWait(browser.driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Download"]'))).send_keys(keys.Keys.ENTER)
logging.debug("Download requested")
time.sleep(5)
wait_for_downloads(browser.driver, download_path, headless=True)
logging.debug("Download completed")
browser.quit()
logging.debug("Browser Quit")
我已经放弃了在无头模式下使用chrome从谷歌驱动器下载的尝试。我切换到Firefox,它使用相同的代码/方法在无头模式下工作。我在Splinter中设置它时遇到了一些问题,所以以下是我的操作方法,以防其他人遇到这个问题,无法让谷歌无头像无头一样工作:
#Solution below from https://github.com/cobrateam/splinter/issues/378#issuecomment-87162477
prof = {}
prof['browser.download.manager.showWhenStarting'] = 'false'
prof['browser.helperApps.alwaysAsk.force'] = 'false'
prof['browser.download.dir'] = download_path
prof['browser.download.folderList'] = 2
prof['browser.helperApps.neverAsk.saveToDisk'] = 'text/csv, application/csv, text/html,application/xhtml+xml,application/xml, application/octet-stream, application/pdf, application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel, application/vnd.ms- excel,application/x-excel,application/x-msexcel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/excel,text/x-c,application/x-7z-compressed,application/zip'
prof['browser.download.manager.useWindow'] = 'false'
prof['browser.helperApps.useWindow'] = 'false'
prof['browser.helperApps.showAlertonComplete'] = 'false'
prof['browser.helperApps.alertOnEXEOpen'] = 'false'
prof['browser.download.manager.focusWhenStarting']= 'false'
browser = splinter.Browser('firefox',profile_preferences=prof, headless=True)
请像下面这样尝试。
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("headless")
driver = webdriver.Chrome(executable_path='C:\temp\chromedriver_win32\chromedriver.exe', options=options)
driver.get('http://www.google.com')