user3214044
user3214044

Reputation: 83

How to click and download with python selenium

I try to download CSV data from GoogleTrend by selenium(python).

In previous, I tried to print source page and extract data that I want later. It worked for some period, but now it does not work.

I try to click download button to got CSV file but nothing happen. Do you have any idea for this case?

Upvotes: 1

Views: 1927

Answers (1)

Dinesh Khanna V
Dinesh Khanna V

Reputation: 45

I have navigated to the link you have provided. If you search for any term, you can see download csv button link will appear at the right side. But there will be 3 download csv buttton links with the same class or css selector are present. So you need to collect all the elements and loop through it so that you can click on specific element. In your case, I assume you want to click on first element. so below code should work. If you want 2nd or 3rd element to click change the index accordingly.

def run_text_extract(search_word):
    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    import time
    profile = webdriver.FirefoxProfile()
    profile.set_preference("browser.download.folderList", 2)
    profile.set_preference("browser.download.manager.showWhenStarting", False)
    profile.set_preference("browser.download.dir", 'C:\\Python27')
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
    driver = webdriver.Firefox(firefox_profile=profile,executable_path=r'C:\\Python27\\geckodriver.exe')
    driver.get("https://trends.google.com/trends/explore?date=all&geo=TH&q="+ search_word)
    time.sleep(7)
    lst = driver.find_elements_by_css_selector(".widget-actions-item.export")
    lst[0].click()

run_text_extract("selenium")

Upvotes: 1

Related Questions