1

I have written some code to click a link to download the course slides via our university course website. My code is bellow:

browser = webdriver.Chrome()

def every_downloads_chrome(driver):
    if not driver.current_url.startswith("chrome://downloads"):
        driver.get("chrome://downloads/")
    return driver.execute_script("""
        var items = downloads.Manager.get().items_;
        if (items.every(e => e.state === "COMPLETE"))
            return items.map(e => e.file_url);
        """)

try:
    print("6. Detail Resource.\n")
    browser.implicitly_wait(10)
    url = "http://course.ucas.ac.cn/access/content/group/155852/1.%E8%AF%BE%E4%BB%B6/CourseInfo.pptx"
    file = browser.find_element_by_xpath((By.XPATH, '//a[@href="'+url+'"]'))
    file.click()
    # waits for all the files to be completed and returns the paths
    paths = WebDriverWait(browser, 120, 1).until(every_downloads_chrome)
    print(paths)
except Exception as e:
    print(e)

The HTML code of the link I wanna click is here:

<a href="http://course.ucas.ac.cn/access/content/group/155852/1.%E8%AF%BE%E4%BB%B6/CourseInfo.pptx" target="_self"><img src="/library/image/sakai/ppt.gif?panel=Main" border="0" alt="PowerPoint " hspace="0">

I run my code, but it shows the error.

Message: invalid argument: 'value' must be a string
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6(255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.4 x86_64)

I have searched the chromedriver official website, but no found chrome driver for 74.0.3729.131. Could somebody help me fix the bug? Thanks in advance!

pandalai
  • 426
  • 4
  • 15

2 Answers2

1

The problem in your code is here.

file = browser.find_element_by_xpath((By.XPATH, '//a[@href="' + url + '"]'))

You have used find_element_by_xpath it takes string value of the locator.That is why the error you are getting.

Message: invalid argument: 'value' must be a string
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6(255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.4 x86_64)

Change above code to.

file = browser.find_element_by_xpath('//a[@href="'+url+'"]')
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

I don't think you will be able to "click" the downloaded document by means of Selenium functions as Chrome Downloads are under ShadowRoot to wit they're exposed as Shadow DOM

enter image description here

Looking here and here reading the inner HTML of the Shadow DOM is possible via getText() function, however you will not be able to interact with the elements normally.

Assuming all above I would recommend simply downloading the necessary file(s) by Python means and executing the associated application(s) or whatever you were going to do with the downloaded file(s)

Dmitri T
  • 159,985
  • 5
  • 83
  • 133