The following code works fine in my system and a zip file is getting downloaded.
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://omayo.blogspot.com/p/page7.html')
time.sleep(2)
elem = driver.find_element(By.XPATH, '//*[@id="post-body-5268577013364905338"]/ul/li/a')
elem.click()
Now i try to download a file from a public website using following code. But it is not getting downloaded. What might be the reason? I even tried headless mode, but it did not solve the issue. I can download the file manually without any issue.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
#from selenium.webdriver.chrome.options import Options
import time
driver = webdriver.Chrome()
driver.get('https://broadbandmap.fcc.gov/data-download/nationwide-data?version=jun2022')
time.sleep(2)
elem = Select(driver.find_element(By.XPATH, '//*[@id="state"]'))
elem.select_by_visible_text('Alabama')
time.sleep(2)
elem1 = driver.find_element(By.XPATH, '//*[@id="content"]/app-nationwide-data/div/div[5]/div[1]/div/table/tbody/tr[1]/td[2]/button/span[1]')
elem1.click()
The reason why the script was not able to download the file could be that it was clicking on the element before it was fully loaded. To solve this issue, an Explicit wait(Webdriver Wait) was added to ensure that the element is clickable before the script proceeds to click it, which resulted in the successful download of the file.
Additionally, the website was blocking the script from downloading the file as it was being detected as a bot. To overcome this, I have used Undetected-Chromedriver instead of the regular Chromedriver. If you are also encountering a similar issue, switching to the Undetected-Chromedriver might be a helpful solution. undetected-chromedriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import undetected_chromedriver
# from selenium.webdriver.chrome.options import Options
import time
# driver = webdriver.Chrome()
driver = undetected_chromedriver.Chrome()
driver.get('https://broadbandmap.fcc.gov/data-download/nationwide-data?version=jun2022')
time.sleep(2)
elem = Select(driver.find_element(By.XPATH, '//*[@id="state"]'))
elem.select_by_visible_text('Alabama')
elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,
'//*[@id="content"]/app-nationwide-data/div/div[5]/div[1]/div/table/tbody/tr[1]/td[2]/button/span[1]')))
elem.click()