Josh Readfern-Grey
Josh Readfern-Grey

Reputation: 33

Selenium not locating xpath element

I'm running Selenium to download a sample PdF file and open it in Chrome but for some reason, I can't manage to get Selenium to click on the first result in the downloads section.

The XPath has been specified but it just won't click on the element, the process is just aborted due to a timeout error (below). Please, does anyone have any ideas of what I am doing wrong?

======================================================================
ERROR: testSearch (__main__.InitialSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Josh/PycharmProjects/ideqcomps/test.py", line 33, in testSearch
    lambda driver: driver.find_elements_by_xpath(downloaded_file_XPath))
  File "/Users/Josh/Library/Python/3.6/lib/python/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


----------------------------------------------------------------------
Ran 1 test in 24.415s

FAILED (errors=1)

Process finished with exit code 1

Here is the code I'm running:

from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

import unittest


class InitialSearch(unittest.TestCase):

    def setUp(self):
        options     = webdriver.ChromeOptions()
        self.driver = webdriver.Chrome(chrome_options=options)
        self.driver.get("https://wetransfer.com/downloads/5451e039a782938826861676664ae91a20171227191356/a45960")

    def testSearch(self):
        driver                   = self.driver
        download_button_xpath    = '//*[@id="file-link"]'
        agree_xpath              = '//*[@id="5451e039a782938826861676664ae91a20171227191356"]/div/div[2]/button'
        downloaded_file_xpath    = '//*[@id="downloads-list"]/downloads-item[1]'
    # Click on agree to terms button
        agree_element = WebDriverWait(driver, 10).until(
            lambda driver: driver.find_element_by_xpath(agree_xpath))
        agree_element.click()
    # Click on download button
        download_button_element = WebDriverWait(driver, 10).until(
            lambda driver: driver.find_element_by_xpath(download_button_xpath))
        download_button_element.click()
    # Go to downloads
        self.driver.get("chrome://downloads/")
    # Click on first item in downloads
        open_pdf_element = WebDriverWait(driver, 10).until(
            lambda driver: driver.find_element_by_xpath(downloaded_file_xpath))
        open_pdf_element.click()


if __name__ == '__main__':
    unittest.main()

Upvotes: 1

Views: 182

Answers (1)

Satish Michael
Satish Michael

Reputation: 2015

I have never used selenium to click on elements from chrome://downloads/ and I don't know if thats possible. I'd leave that to the experts here.

Having said that, If you end up changing the way you open the downloaded pdf, you can try the below approach

options = webdriver.ChromeOptions()
options .add_experimental_option("prefs", {
    "download.default_directory": '<temp_download_dir>',
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True
})

this will force the chrome to download the pdf to temp_download_dir, from there you can open the file

Upvotes: 1

Related Questions