Home > OS >  Why selenium is not clicking on the submenu in the drop down
Why selenium is not clicking on the submenu in the drop down

Time:10-15

Here is the code I'm trying to use to scrape data from FRED website to download the time series data in CSV format but it is redirecting me top another page

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

url='https://fred.stlouisfed.org/series/TERMCBAUTO48NS'
driver=webdriver.Chrome(executable_path=r'D:\\Workspace\\Python\\automation\\chromedriver.exe') 
driver.get(url)
element=driver.find_element_by_id('download-button')
element.click()

wait1=WebDriverWait(driver,20)
result1=wait1.until(
    EC.element_to_be_clickable((By.ID,'fg-download-menu')))
print('Result 1: ',result1)

menu=driver.find_element_by_id('fg-download-menu')

wait1=WebDriverWait(driver,20)
result2=wait1.until(
    EC.element_to_be_clickable((By.ID,'download-data-csv')))
print('Result 2: ',result2)
hidden_submenu=driver.find_element_by_id('download-data-csv')

actions=ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()

driver.quit()

CodePudding user response:

This should work:

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

driver = webdriver.Chrome("D:/chromedriver/94/chromedriver.exe")
driver.get("https://fred.stlouisfed.org/series/TERMCBAUTO48NS")
# wait 60 seconds 
wait = WebDriverWait(driver,60)
wait.until(EC.element_to_be_clickable((By.ID, "download-button"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "download-data-csv"))).click()

CodePudding user response:

The locators you are using are not unique. Like there are several tags with the same id download-button.

Its important to find unique locators for Elements. You can refer This link

Try like below and confirm:

driver.get("https://fred.stlouisfed.org/series/TERMCBAUTO48NS")
wait = WebDriverWait(driver,30)

# Click on Download button
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@id='page-title']//button[@id='download-button']"))).click()

# Click on CSV data
wait.until(EC.element_to_be_clickable((By.XPATH,"//ul[@id='fg-download-menu']//a[@id='download-data-csv']"))).click()
  • Related