0

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': 'xyz@outlook.com',
        }

        time.sleep(3)
        FormPage().fill_form(data).submit()
Cashi
  • 59
  • 1
  • 4

1 Answers1

1

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.

Andreas
  • 221
  • 1
  • 11
  • Thank you for clarifying Andreas. Although now I receive an assertion error while running the code, I did inspect the page source before including the "resources" in the driver.title. Am I missing out on something here? – Cashi Oct 17 '17 at 06:19
  • I simply commented that line, as I was not sure what your intention with the assert was. Please clarify your intention if this is a feature. – Andreas Oct 17 '17 at 06:33
  • Hi Andreas, The intention is to locate and download all xml links in the webpage. I've updated the code now, but face issue with CSS selector https://stackoverflow.com/questions/46784644/python-selenium-css-selector-element-is-not-visible – Cashi Oct 17 '17 at 07:36