简体   繁体   中英

Selenium chrome headless cannot execute onclick event on Linux server

I am having trouble porting over a Python Selenium headless google chrome script from Windows to a Linux server that navigates to a website and downloads a file. The script runs without errors on windows and on a linux server, however the script on the linux server never downloads a file.

I am not sure what may be the issue. I thought it may be a permission issue, but I ran a small script using the wget package and it downloaded the file in to the specified folder.

What do you think may be preventing the same script from downloading a file on the linux server? The server has the latest chrome and chromedriver installed (Version 81) and my program correctly points to them.

This script works and downloads the icon

import wget
url = "https://www.python.org/static/img/python-logo@2x.png"
wget.download(url, '/external/gen_dir')

This script runs and shuts the driver but never downloads anything(Works in Windows on my personal computer when pointed to the correct folders on my local machine). There are no script breaking errors Has same download location as above script so I do not think it is a permissions issue. Both scripts were executed from the same folder.

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')

It seems it's because driver.quit() ran before download started, so

search_input.click()
time.sleep(5)

driver.quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM