Home > Mobile >  How to click on a download button with Selenium
How to click on a download button with Selenium

Time:10-23

I try to scrap some informations on a website. There is my code

from lib2to3.pgen2 import driver
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
import pyvirtualdisplay

class Informations:
    def __init__(self):
        self.list_site_by_siren = ["418 096 392", "334 992 798"]
        self.website = "https://www.pappers.fr/"

def main():
    informations = Informations()
    print(informations.list_site_by_siren)
    
    driver = webdriver.Chrome()
    driver.set_window_size(1920,1500)

    driver.get(informations.website)

    driver.find_element(By.XPATH, "//input[@placeholder='Entreprise, N° SIREN, Dirigeant, Mot-clé...']").send_keys(informations.list_site_by_siren[0])


    driver.find_element(By.XPATH, '//button[text()="Rechercher"]').click()

    time.sleep(1)
    driver.find_element(By.XPATH, '//button[contains(text(),"Voir les comptes")]').click()
    #driver.find_element(By.CLASS_NAME, "button mt2").click() same but doesnt work too

my driver.find_element(By.CLASS_NAME, "button mt2").click() doesn't work.

enter image description here

i would like to click on "Voir les comptes" and start the force the download of the file but i don't know how to do that.

this is the html code

<a href="micromania-418096392/comptes/MICROMANIA - Comptes sociaux 2022 19-07-2022.pdf" title="Voir les comptes 2022-01-31 de MICROMANIA" target="_blank" >VOIR LES COMPTES <span  style="margin-left: 4px;"></span></a>

So at first i have to locate micromania-418096392/comptes/MICROMANIA - Comptes sociaux 2022 19-07-2022.pdf and the second part is to download it but i dont find how i can do that and actually it doesn't detect the button.

Thanks for yours answers!

CodePudding user response:

this line can get you nothing: driver.find_element(By.CLASS_NAME, "button mt2").click() because you actually look not for this "button mt2" but for this " 'button mt2' ", it tries to find class with name 'button mt2' (=it looks for one name, not for two ones). of course there is no class with such name and it finds nothing.

solutions:

  1. you can try to find all 'buttons' (or all 'mt2') by find_elements() (..ntS) and iterate to what you need ->driver.find_elements(By.CLASS_NAME, "button")
  2. you can use xpath() or something else (but not By.Class parameter)

CodePudding user response:

Try the below xpath to get the button -

//a[contains(@title,'Voir les comptes')]/span[@class='fas fa-download']

CodePudding user response:

driver.get(driver.find_element(By.CLASS_NAME, "button mt2").get_attribute("href"))

  • Related