1

I am trying to write code to check download is completed by selenium and chrome driver. My idea is

1.Go to download page("chrome://downloads/")

2.Check the url to ensure we have downloaded file from that site (locate http://xxxxxxxx)

3.Check download status( If I found "show in folder", it implies download success. if not, download failed)

I am stucking in step2, when I try to locate the url. I used developer tools, move cursor to the url to locate the element,and then I right click and copy the xpath. The xpath is like this

          //*[@id="file-link"]

And then I try to click ctrl+F in developer tool and paste the xpath again. I cannot locate the element. Why? Checked there is no frame.

  • Does this answer your question? [Accessing Shadow DOM tree with Selenium](https://stackoverflow.com/questions/23920167/accessing-shadow-dom-tree-with-selenium) – Ramon Medeiros Jan 08 '20 at 15:12

3 Answers3

0

It shadow DOM and need to select /deep/ using CSS selector

downloads-manager/deep/downloads-item/deep/a[id="file-link"]
ewwink
  • 18,382
  • 2
  • 44
  • 54
0

Another way of doing this could be writing a small Java utility for the same using File Class. Something like this:

 File f = new File("C:\\Users\\username\\Downloads\\Users_" + fielName+ ".xls");
    Assert.assertTrue(f.exists());
Neha
  • 316
  • 1
  • 4
  • 16
0

This works for me in Python:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

# disable file preview
options = webdriver.ChromeOptions()
options.add_experimental_option(
    'prefs', {
        'profile.default_content_settings.popups': 0,
        'download.default_directory': '/some/download/dir',
        'download.prompt_for_download': False,
        'download.directory_upgrade': True,
        'plugins.always_open_pdf_externally': True,
        'plugins.plugins_disabled': 'Chrome PDF Viewer',
        'disable-popup-blocking': True
    }
)
driver = webdriver.Chrome(options=options)

# start load file
driver.get('url-to-file')


def condition_load_file(dr: webdriver.Chrome):
    return dr.execute_script("""
    try {
        var el = document.querySelector("body > downloads-manager").shadowRoot.querySelector("#frb0").shadowRoot.querySelector("#show");
        return el !== null;
    } catch(exc) {
        return false;
    }
    """)


# open new window and go to chrome://downloads/
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get('chrome://downloads/')

# waiting for download to finish
WebDriverWait(driver, 120).until(condition_load_file)
S.B
  • 13,077
  • 10
  • 22
  • 49
Egor Crane
  • 33
  • 3