Cashi
Cashi

Reputation: 59

Python Selenium Webdriver: Enumerate hyperlinks and download files

I'm trying to enumerate all hyperlinks (with files to download from) from a webpage, and subsequently download these files one after the other. The hyperlinks when clicked require a form to be filled for which I've created a class to complete the same. I receive "AttributeError: 'tuple' object has no attribute 'click'" during the run of the code. I've hereby attached the code, any suggestion to rectify this will much appreciated.

import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", "F:\Projects\Poli_Map\DatG_Py_Dat")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/xml")

driver = webdriver.Firefox(firefox_profile=fp)
driver.get('https://data.gov.in/catalog/variety-wise-daily-market-prices-data-cauliflower')

assert "resources" in driver.title
continue_link = driver.find_element_by_tag_name('a')
elem = driver.find_elements_by_xpath("//*[@href]")
z = elem

for links in enumerate(z):
    driver.implicitly_wait(4)
    for link in enumerate(links):
        link.click()


        class FormPage(object):
            def fill_form(self, data):
                driver.execute_script("document.getElementById('edit-download-reasons-non-commercial').click()")
                driver.execute_script("document.getElementById('edit-reasons-d-rd').click()")
                driver.find_element_by_xpath('//input[@name = "name_d"]').send_keys(data['name_d'])
                driver.find_element_by_xpath('//input[@name = "mail_d"]').send_keys(data['mail_d'])
                return self

            def submit(self):
                driver.execute_script("document.getElementById('edit-submit').click()")


        data = {
            'name_d': 'xyz',
            'mail_d': '[email protected]',
        }

        time.sleep(3)
        FormPage().fill_form(data).submit()

Upvotes: 0

Views: 468

Answers (1)

Andreas
Andreas

Reputation: 221

Please check your usage of enumerate, which is plainly wrong. Given

elem = driver.find_elements_by_xpath("//*[@href]")

you can now simply iterate over this collection, such as:

for link in elem:
    link.click()

All the rest you do not need.

Upvotes: 1

Related Questions