【发布时间】:2020-08-30 12:04:23
【问题描述】:
我在将 Python Selenium 无头 google chrome 脚本从 Windows 移植到导航到网站并下载文件的 Linux 服务器时遇到问题。该脚本在 windows 和 linux 服务器上运行没有错误,但是 linux 服务器上的脚本从不下载一个文件。
我不确定可能是什么问题。我认为这可能是权限问题,但我使用 wget 包运行了一个小脚本,并将文件下载到指定的文件夹中。
您认为是什么阻止了同一个脚本在 linux 服务器上下载文件?服务器安装了最新的 chrome 和 chromedriver(版本 81),我的程序正确指向它们。
此脚本运行并下载图标
import wget
url = "https://www.python.org/static/img/python-logo@2x.png"
wget.download(url, '/external/gen_dir')
此脚本运行并关闭驱动程序,但从不下载任何内容(当指向本地计算机上的正确文件夹时,可在我的个人计算机上的 Windows 中运行)。没有脚本破坏错误 与上述脚本具有相同的下载位置,所以我认为这不是权限问题。两个脚本都是从同一个文件夹中执行的。
from selenium import webdriver
print('packages imported')
from selenium.webdriver.chrome.options import Options
print('options imported')
#specifying headless and download options
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(executable_path=r'team/mm/chromedriver', options=options)
#'/external/gen_dir' directory for sample data
params = {'behavior': 'allow', 'downloadPath': r'/external/gen_dir'}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)
print('download paramaters executed')
driver.get("https://www.thinkbroadband.com/download")
# initialize an object to the location on the html page and click on it to download
search_input = driver.find_element_by_css_selector('#main-col > div > div > div:nth-child(8) > p:nth-child(1) > a > img')
search_input.click()
driver.quit()
print('driver closed')
【问题讨论】:
-
能否请您使用 javascript 执行器来执行对该元素的点击。
标签: python linux selenium google-chrome selenium-chromedriver