Fail to download file with opened new tab in headless mode on Linux

大兔子大兔子 提交于 2020-01-30 11:21:22

问题


As this answer by myself: How to download files headless in Selenium (Java) when download happens in new tab?

If download button trigger download action in opened new tab, I will switch to new tab and send command(as below code) to download file.

def enable_download_in_headless_chrome(self, driver, download_path):
    driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {
        'cmd': 'Page.setDownloadBehavior',
        'params': {'behavior': 'allow', 'downloadPath': download_path}
    }

    driver.execute("send_command", params)

I found that the above method failed on linux occasionally.

When driver got the error, it would be crashed for a long time. (I detect the situation from my error handling with retry.)

Error message:

Message: timeout
(Session info: headless chrome=77.0.3865.120)

And I have looked at solution with the issue discussion but no one can solve it: https://bugs.chromium.org/p/chromium/issues/detail?id=696481

I guess the root cause is that I send enable download command before the new tab in ready state?

Please help me to find the solution. Thanks.


回答1:


if there is a new tab is opened when you click on a download link (for example if it has target="_blank" attribute ). In such case download in headless with the enable_download_in_headless_chrome method, a solution doesn't work. So you can remove target="_blank" attribute by JS or get href and try to download by direct opening the link in the same tab.

if there is link and open in new tab then you can open in the same tab by overwriting javascript

def open_link_same_tab_download_file(current_user_driver, element):
    # open element in same tab add target to self
    current_user_driver.execute_script('arguments[0].target="_self"',element)

    # click on element to download file
    current_user_driver.execute_script("arguments[0].click()", element

if there is no link attribute then you can override javascript of open window in new tab like below

def open_new_tab_download_file(current_user_driver, element):
    # open element in same tab override javascript for that
    current_user_driver.execute_script('window.open = function(url) {window.location=url}')

    # click on element to download file
    current_user_driver.execute_script("arguments[0].click()", element)



回答2:


On Linux, I found that the file will be downloaded two times through below code:

# Open new tab by clicking download button
download_button.click()
# Switch to new tab
driver.switch_to.window(driver.window_handles[-1])
# Wait for ready state 
WebDriverWait(driver, 60).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')

##### The file will be downloaded to original download path ##### 

# Change download directory
enable_download_in_headless_chrome(driver, download_path)
# Refresh to trigger download behavior again
driver.refresh()

##### The file will be downloaded to your specific path again #####

If send "enable download" command before ready state, it cannot change download directory successfully.



来源:https://stackoverflow.com/questions/59676832/fail-to-download-file-with-opened-new-tab-in-headless-mode-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!