1

Tried to download with Selenium and Python , the excel file by clicking the button on page but failed. Following is the class extracted from page:

<div class="tabPanel_func ts2_icon_map ts2_icon_excel" onclick="ExportExcel('tblTS2', tsData);"\>\</div\>`

I also want to download the file and then save to the specific drive location.

Following is my code :

url = "http://www.aastocks.com/tc/stocks/market/high-low-stocks.aspx?catg=1&period=3&t=1"
driver = webdriver.Chrome()
driver.get(url)

Code trials:

driver.find_element(By.XPATH, "//\[contains(@onclick, ExportExcel('tblTS2', tsData)')\]")` 

or

driver.find_element(By.XPATH, "//\*\[@class= 'tabPanel_func ts2_icon_map ts2_icon_excel'\]")` 

or

driver.find_element(By.CSS_SELECTOR, "//button\[contains(@onclick,'ExportExcel('tblTS2', tsData)')\]").click()`

All approaches have failed.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ed T
  • 15
  • 3

3 Answers3

0

Try the below XPath expression:

//*[contains(@class,'tabPanel_func ts2_icon_map ts2_icon_excel')]

XPath explanation: Searches the entire DOM(//*) which contains attribute = class and its value = tabPanel_func ts2_icon_map ts2_icon_excel

Above XPath expression will locate the below element:

enter image description here

Below is the full line of code to click the Export button:

driver.find_element(By.XPATH,"//*[contains(@class,'tabPanel_func ts2_icon_map ts2_icon_excel')]").click()

Shawn
  • 4,064
  • 2
  • 11
  • 23
0

Try following xpath to click on export excel.

//div[contains(@onclick,'ExportExcel')] 

code:

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[contains(@onclick,'ExportExcel')] 
"))).click()
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CLASS_NAME:

    driver.get('http://www.aastocks.com/tc/stocks/market/high-low-stocks.aspx?catg=1&period=3&t=1')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "ts2_icon_excel"))).click()
    
  • Using CSS_SELECTOR:

    driver.get('http://www.aastocks.com/tc/stocks/market/high-low-stocks.aspx?catg=1&period=3&t=1')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.ts2_icon_excel[onclick^='ExportExcel']"))).click()
    
  • Using XPATH:

    driver.get('http://www.aastocks.com/tc/stocks/market/high-low-stocks.aspx?catg=1&period=3&t=1')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@onclick, 'ExportExcel')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser snapshot:

ExportExcel

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • It works, thanks ! What if I want to download the file to a specific drive and location instead of download to my C drive "Download" ? Do you have any suggestions on good books which cover these knowledge that I can read and learn other than stack overflow? – Ed T Feb 09 '23 at 15:16
  • I tried '''options.add_experimental_option("prefs", {"download.default_directory":r"D:/"}) s = Service('chromedriver.exe') driver = webdriver.Chrome(service=s, options=options)''' but failed to download. – Ed T Feb 09 '23 at 15:59
  • Sounds like a separate question. Feel free to raise a new question with your new requirement. – undetected Selenium Feb 09 '23 at 17:39
  • 1
    Thanks ! Will do and raise new question for this . – Ed T Feb 10 '23 at 09:34