Py1996
Py1996

Reputation: 239

File is not getting downloaded using selenium chromedriver

I am trying to download some csv files using selenium webdriver from dynamic loaded website where i need to click buttons to get my file downloaded. However when i run my python script in my local machine all files will get downloaded but same script wont download files in AWS Linux server machine. It neither gives me an error during the downloads.

arguments I added for driver

options = Options()
options.binary_location = GoogleChrome_Path
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_experimental_option("prefs", {"download.default_directory": SomeFolder,
                                          "download.prompt_for_download": False,
                                          "download.directory_upgrade": True,
                                          "safebrowsing.enabled": True
                                          })

And right after this i will perform some click operations to download the file and as i have stated above when it executes in the Linux server machine the files wont get downloaded and also the script does not throw any error

How do i know that file wont get downloaded? well, I check the files in save path folder using

os.listdir(SomeFolder)

Is there any way that I can find out what is causing this issue

Element tag that I am trying to click

<a title="CSV (comma delimited)" alt="CSV (comma delimited)" onclick="$find('ctl_ReportViewer').exportReport('CSV');" href="javascript:void(0)" style="color: rgb(51, 102, 204); font-family: Verdana; font-size: 8pt; padding: 3px 8px 3px 32px; display: block; white-space: nowrap; text-decoration: none;">CSV (comma delimited)</a>

Source URL https://apps.who.int/flumart/Default?ReportNo=12

select random drop down values and click display report and it takes some seconds to load the report. after completely loaded we can see blue color button(Export drop down menu) click that button and export to csv

I am just having problem while clicking export as a csv.

enter image description here

Upvotes: 2

Views: 2177

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

You can try the below CSS_SELECTOR :

a[title^='CSV (comma delimited)'][onclick*='CSV']

Using Explicit waits :

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title^='CSV (comma delimited)'][onclick*='CSV']"))).click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Directly use driver.find_element

driver.find_element(By.CSS_SELECTOR, "a[title^='CSV (comma delimited)'][onclick*='CSV']").click()

If these 2 do not work, try JS :-

button = driver.find_element_by_css_selector("a[title^='CSV (comma delimited)'][onclick*='CSV']")
driver.execute_script("arguments[0].click();", button)

Upvotes: 1

Related Questions