1

There is a link on a website that downloads a csv file. The link is in a table but the actual download link is hidden.

<div id="testjs">
    <div test-example="">
        <table test-example="">
            <tr test-example="">
                <th test-example="">Car</th>
                <th test-example="">File</th>
            </tr>
            <tr test-example="">
                <td test-example="">Ford</td>
                <td test-example="">
                    <a test-example="" href="#">ford.csv</a>
                </td>
            </tr>
        </table>
    </div>
</div>

I'm trying automate file download by scraping the site using python/selenium.

from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get("https://www.example.com")
driver.find_element_by_link_text('ford.csv')

When the last line above runs the script returns:

<selenium.webdriver.remote.webelement.WebElement (session="<example session string>", element="<example element string>")>

When I run the code below nothing happens:

driver.find_element_by_link_text('ford.csv').click()

How do I get the file to download?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
chris
  • 148
  • 8
  • Use Chrome Headless browser instead of PhantomJS – KunduK Nov 10 '19 at 20:27
  • Have you got any solution yet for your problem using chrome? – KunduK Nov 13 '19 at 16:28
  • I was partially able to solve my issue using chrome driver but not headless. DebanjanB’s solution did work. However there was a ‘allow cookies’ pop up that was getting in the way of the download link that I had to click first. The problem with headerless chrome was nothing downloaded. When chrome was not headless the file downloaded. – chris Nov 13 '19 at 20:07
  • Check this answer for headless chrome see if that helps https://stackoverflow.com/questions/58770745/select-drop-down-option-using-selenium-headless-chrome-python/58772145#58772145 – KunduK Nov 13 '19 at 21:46

2 Answers2

1

Apparently there isn't any issue with the following line of code:

driver.find_element_by_link_text('ford.csv')

However at this point it is worth to mention that the dot character . always have a special effect/meaning.


Assuming you intend to click() on the element with text as ford.csv which is adjacent to the element with text as Ford, as a solution you can:

  • Split the text ford.csv into two parts ford and csv and use within a
  • As you intent to invoke click(), you have to induce WebDriverWait for the element_to_be_clickable()
  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "tr[test-example] td:nth-child(2)>a[test-example]"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'ford') and contains(.,'csv')]"))).click()
      
    • Note : You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • With these approaches I get the same error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (X, Y). Other element would receive the click: – chris Nov 11 '19 at 18:24
  • To address ElementClickInterceptedException follow the discussions 1)[selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python](https://stackoverflow.com/questions/57741875/selenium-common-exceptions-elementclickinterceptedexception-message-element-cl/57749551#57749551) 2) [Element MyElement is not clickable at point (x, y)… Other element would receive the click](https://stackoverflow.com/questions/44724185/element-myelement-is-not-clickable-at-point-x-y-other-element-would-receiv/44724688#44724688) – undetected Selenium Nov 12 '19 at 06:28
  • 1
    Thanks the issue was a ‘allow cookies’ pop up that was getting in the way. So I used your solution to allow cookies first and then download the file. – chris Nov 13 '19 at 20:09
0

PhantomJS is not supported in new Selenium version, consider user other driver. For your problem there were a bug clicking Button with JS.

Try this, I found in my selenium library to click_js buttons

item = driver.find_element_by_link_text('ford.csv')
ActionChains(driver).move_to_element(item).click().perform()
Wonka
  • 1,548
  • 1
  • 13
  • 20
  • Ok i've tried using this approach with headerless chrome but nothing happens. Got any ideas? – chris Nov 11 '19 at 18:19