简体   繁体   中英

How to click a button with Selenium chromedriver in Python

Hi Guys (sorry in advanced for my english), I want to click a button in order to download a file from this page https://myterna.terna.it/SunSet/Public/Pubblicazioni?filter.IdSezione=585AF7EFCA196EBBE0532B889B0A6372 . This is my simple code:

download_dir = 'C:/Users/Francesco.Borri/Desktop/Sunset'

options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : download_dir}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=options)
driver.get('https://myterna.terna.it/SunSet/Public/Pubblicazioni?filter.IdSezione=585AF7EFCA196EBBE0532B889B0A6372')

container1 = driver.find_element_by_class_name("row") #Until this class is OK
container2 = driver.find_element_by_class_name("col-sm-12 table-wrapper") # From this class NOPE

#button = container.find_element_by_class_name(CLASS_OF_THE_BUTTON)
#button.click()

The code opens the page correctly, but It's not able to find all the classes I'm looking for (in which inside one of them there is my precious button). I studied the html page source and I found that the classes initially missing are created from a script (screenshot below) when the page is loaded. I'm not familiar with Javascript and Ajax. Is there a way to find and to click my precious button?

PS: Now the file is downloaded but with this undefined error. I tried even to download it manually and I receive the same error. It looks that my chromedriver doesnt have the permissions to download files.
在此处输入图像描述

Because there are many download buttons on the page, you will need to specify the title of the publication you wish to download when looking for your button. It is also recommended to invoke WebDriverWait on the element you are trying to click to ensure that it is visible before interacting with it:

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

download_dir = 'C:/Users/Francesco.Borri/Desktop/Sunset'

options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : download_dir}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=options)
driver.get('https://myterna.terna.it/SunSet/Public/Pubblicazioni?filter.IdSezione=585AF7EFCA196EBBE0532B889B0A6372')


# Article name is "Prezzi Giornalieri Marginale Quarto Orari 20191111"

# wait on the download button for this article to exist
download_button = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//tr[td/span[text()='Prezzi Giornalieri Marginale Quarto Orari 20191111']]/td/div/a[contains(@class, 'download')]")))

# click the link to download
download_button.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM