Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
910 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...