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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…