IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Selenium : 2 tables à trouver [Python 3.X]


Sujet :

Python

  1. #1
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut Selenium : 2 tables à trouver
    Bonsoir,
    J'ai 2 tables à récupérer.
    Je parviens à récupérer la première et j'ai l'impression d'être neuneu : la deuxième n'est pas récupérée.
    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    tab_details_ciel = driver.find_elements(by=By.XPATH,value="//table[@id='statistiques_6451']/tbody/tr")
     
    tab_details_ciel2 = driver.find_elements(by=By.XPATH,value="//table[@id='statistiques2_6451']/tbody/tr")
    Est-ce à cause de la structure qui fait que lorsque j'ai parcouru la première, je ne peux sortir de l'arborescence ?
    Quelque chose m'échappe manifestement.

  2. #2
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 203
    Par défaut
    Hello,
    comment se présente le HTML ? il y a bien deux tables avec les id indiqués ?
    Ami calmant, J.P

  3. #3
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    Oui, elles sont l'une en-dessous de l'autre. Les ID sont bonnes.

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <div class="row btable-row1"><div id="btable_statistiques_6451_caption" ...
    		<thead>
     
    		</thead>
    		<tbody>
     
     
    		<tr class="odd" role="row">
     
    					<td class=" text-left">1</td>
    				</tr></tbody>
    	</table><div id="statistiques_6451_processing" class="dataTables_processing panel panel-default" style="display: none;">Traitement en cours...</div></div></div><div class="row btable-row4"><div id="btable_statistiques_6451_info" class="col-sm-5"></div><div id="btable_statistiques_6451_pagination_bottom" class="col-sm-7"></div></div>
    	</table><div id="statistiques_6451_processing" class="dataTables_processing panel panel-default" style="display: none;">Traitement en cours...</div>
    C'est un truc comme cela avec 2 tables l'une en-dessous de l'autre.

  4. #4
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 203
    Par défaut
    Hello,
    Cela m'étonnerait que cela soit comme cela car :
    L'attribut universel id définit un identifiant qui doit être unique pour l'ensemble du document.
    Essaie de me montrer le bout du code HTML contenant les deux tables que tu veux capturer en utilisant les outils de développement de ton navigateur qui te montre le code HTML exact de ce qui est affiché dans celui-ci (ex dans Chrome Plus d'outils/Outils de développement).
    Ami calmant, J.P

  5. #5
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    Oui, la deuxième table a comme iId : statistiques2...
    J'essaie de voir cela cet après-midi.

  6. #6
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    J'ai un truc comme cela (j'ai épuré un peu) :
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    	<br> 
     
    	<h2 class="mt-1 mb-1 ml-1">...</h2>
    	<div id="statistiques_6451_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer"><div class="row btable-row1"><div id="btable_statistiques_6451_caption" class="col-sm-8"></div><div id="btable_statistiques_6451_length" class="col-sm-4"></div></div><div class="row btable-row2"><div id="btable_statistiques_6451_filter" class="col-sm-6"><div id="statistiques_6451_filter" class="dataTables_filter" style="float: left;"><label>Rechercher&nbsp;:<input type="search" class="form-control input-sm" placeholder="" aria-controls="statistiques_6451" style="margin: 0px 10px 0px 0px; background-color: rgb(255, 255, 255); border-color: rgb(204, 204, 204); border-width: 1px;"></label></div><div class="dt-buttons btn-group"></div><div class="btn-group" id="statistiques_6451_btn_export" role="group"><button title="Exporter le tableau" type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">Export</button><div id="statistiques_6451_export" class="dropdown-menu dropdown-export"><a class="btn btn-default buttons-csv buttons-html5 btn-sm btn-export-format" tabindex="0" aria-controls="statistiques_6451" href="#"><span><i class="fa fa-file-text-o" aria-hidden="true" style="font-size:16px"></i> CSV</span></a><a class="btn btn-default buttons-excel buttons-html5 btn-sm btn-export-format" tabindex="0" aria-controls="statistiques_6451" href="#"><span><i class="fa fa-file-excel-o" aria-hidden="true" style="font-size:16px"></i> Excel</span></a><a class="btn btn-default buttons-print btn-sm btn-export-format" tabindex="0" aria-controls="statistiques_6451" href="#"><span><i class="fa fa-print" aria-hidden="true" style="font-size:16px"></i> Impression</span></a></div></div><button title="Reinitialiser le tableau" class="btn btn-default btn-sm" id="statistiques_6451_btn_reinitialiser"><span class="glyphicon glyphicon-repeat"></span> Reinitialiser</button></div><div id="btable_statistiques_6451_pagination" class="col-sm-6"></div></div><div class="row btable-row3"><div id="btable_statistiques_6451_table" class="col-sm-12"><table class="btable table table-bordered table-condensed table-striped table-hover no-footer dataTable" id="statistiques_6451" data-table-pagination="false" data-table-export="true" data-table-page-info="false" data-table-fixheader="false" data-table-export-nom="Total des types de bac actuels par voeux" data-table-export-format="csv,excel,print" role="grid" style="visibility: visible;">
    		<thead>
    			<tr role="row"><th data-colonne-filtre="select" id="0" class="text-left sorting" tabindex="0" aria-controls="statistiques_6451" rowspan="1" colspan="1" aria-label="...</th></tr>
                    </thead>
                    <tbody>
                             
                            A RECUPERER     
                                    
                    <tr class="odd" role="row">
     
                                    </tr></tbody>
            </table><div id="statistiques_6451_processing" class="dataTables_processing panel panel-default" style="display: none;">Traitement en cours...</div></div></div><div class="row btable-row4"><div id="btable_statistiques_6451_info" class="col-sm-5"></div><div id="btable_statistiques_6451_pagination_bottom" class="col-sm-7"></div></div></div>
            <br>
            
            <h2 class="mt-1 mb-1 ml-1">...</h2>
            <div id="statistiques2_6451_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer"><div class="row btable-row1"><div id="btable_statistiques2_6451_caption" class="col-sm-8"></div><div id="btable_statistiques2_6451_length" class="col-sm-4"></div></div><div class="row btable-row2"><div id="btable_statistiques2_6451_filter" class="col-sm-6"><div id="statistiques2_6451_filter" class="dataTables_filter" style="float: left;"><label>Rechercher&nbsp;:<input type="search" class="form-control input-sm" placeholder="" aria-controls="statistiques2_6451" style="margin: 0px 10px 0px 0px; background-color: rgb(255, 255, 255); border-color: rgb(204, 204, 204); border-width: 1px;"></label></div><div class="dt-buttons btn-group"></div><div class="btn-group" id="statistiques2_6451_btn_export" role="group"><button title="Exporter le tableau" type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">Export</button><div id="statistiques2_6451_export" class="dropdown-menu dropdown-export"><a class="btn btn-default buttons-csv buttons-html5 btn-sm btn-export-format" tabindex="0" aria-controls="statistiques2_6451" href="#"><span><i class="fa fa-file-text-o" aria-hidden="true" style="font-size:16px"></i> CSV</span></a><a class="btn btn-default buttons-excel buttons-html5 btn-sm btn-export-format" tabindex="0" aria-controls="statistiques2_6451" href="#"><span><i class="fa fa-file-excel-o" aria-hidden="true" style="font-size:16px"></i> Excel</span></a><a class="btn btn-default buttons-print btn-sm btn-export-format" tabindex="0" aria-controls="statistiques2_6451" href="#"><span><i class="fa fa-print" aria-hidden="true" style="font-size:16px"></i> Impression</span></a></div></div><button title="Reinitialiser le tableau" class="btn btn-default btn-sm" id="statistiques2_6451_btn_reinitialiser"><span class="glyphicon glyphicon-repeat"></span> Reinitialiser</button></div><div id="btable_statistiques2_6451_pagination" class="col-sm-6"></div></div><div class="row btable-row3"><div id="btable_statistiques2_6451_table" class="col-sm-12"><table class="btable table table-bordered table-condensed table-striped table-hover no-footer dataTable" id="statistiques2_6451" data-table-pagination="false" data-table-export="true" data-table-page-info="false" data-table-fixheader="false" data-table-export-nom="Total des types de bac par voeux" data-table-export-format="csv,excel,print" style="visibility: visible;" role="grid">
                    <thead>
                            <tr role="row"><th data-colonne-filtre="select" id="0" class="text-left sorting" tabindex="0" aria-controls="statistiques2_6451" rowspan="1" colspan="1" aria-label="Type de bac: activer pour trier la colonne par ordre croissant">Type de bac<select class="form-control filtre-select" id="statistiques2_6451_filtre_0" style="border-color: rgb(204, 204, 204); border-width: 1px;"><option value="" <="" option=""></option><option value=...</option></select></th><th data-colonne-filtre="texte" id="1" class="text-left sorting" tabindex="0" aria-controls="statistiques2_6451" rowspan="1" colspan="1" aria-label="... aria-label="Non Boursiers certifiés des lycées: activer pour trier la colonne par ordre croissant">Non Boursiers certifiés des lycées</th></tr>
    		</thead>
    		<tbody>	
     
    		<tr role="row" class="odd">
    A RECUPERER
    				</tr></tbody>
    	</table><div id="statistiques2_6451_processing" class="dataTables_processing panel panel-default" style="display: none;">Traitement en cours...</div></div></div><div class="row btable-row4"><div id="btable_statistiques2_6451_info" class="col-sm-5"></div><div id="btable_statistiques2_6451_pagination_bottom" class="col-sm-7"></div></div></div>
    	<br>
     
    	<div id="bouton">
    		<table class="bouton">
    	<tbody><tr>
    		<td class="gauche"></td>
    		<td class="milieu"><a href="statistiques?ACTION=1" class="bouton">Retour</a>		</td>
    		<td class="droite"></td>
    	</tr></tbody></table>
     
    	</div>
     
    </div>
    	</div>

  7. #7
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 203
    Par défaut
    je ne sais pas ce que tu as épuré mais dans ce que tu nous mets A récupérer c'est un div qui a pour id statistiques2_6451_processing pas une table.
    Dans la zone que tu nous mets à récupérer il n'y a qu'une table de classe bouton (</table> c'est une fin de balise pas un début, il faut regarder ce qu'il y a dans les balises qui commencent par <table )

  8. #8
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    En copiant, j'ai du mal à garder les infos.
    Voici une copie d'écran.Nom : Capture d’écran du 2024-01-28 14-27-11.png
Affichages : 235
Taille : 147,9 Ko

  9. #9
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 203
    Par défaut
    OK
    dans le code de ton premier message est-ce que tab_details_ciel2 possède une valeur après affectation et que fais tu après dans ton code. Et je vois dans ton code HTML qu'il y a des traitements en cours. Attends tu que les traitements soient terminés avant de capturer les tables ?

  10. #10
    Expert confirmé Avatar de papajoker
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2013
    Messages
    2 322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2013
    Messages : 2 322
    Par défaut
    bonjour

    On ne sais pas trop ce que tu fais !
    Il faudrait plutôt écrire un petit script de test simple qui te permet de valider ton "parsing" et qui nous permet en plus de tester.
    Par exemple (A adapter suivant ta config)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    from pathlib import Path
    from selenium import webdriver
    from selenium.webdriver.common.by import By
     
    from selenium.webdriver.chrome.service import Service
    service = Service(executable_path="/home/patrick/.local/bin/chromedriver")
     
     
    CONTENT = """<html>
      <body>
        <div><br>
        <div><table id="statistiques_6451"><tbody><tr><td>1</td></tr><tr><th>01</th></tr></tbody></table></div>
        <table id="statistiques2_6451"><tbody><tr><td>2</td></tr></tbody></table>
        </div>
      </body>
    </html>
    """
    FILE_ = Path.home() / "toto.html"
    FILE_.write_text(CONTENT)
    print(FILE_)
     
    driver = webdriver.Chrome(service=service)
    try:
        driver.get(f"file://{FILE_}")
        print(driver.page_source)
     
        for tag in driver.find_elements(By.XPATH, "//table[@id='statistiques_6451']/tbody/tr"):
            print("statistiques_6451", tag, tag.text)
        for tag in driver.find_elements(By.XPATH, "//table[@id='statistiques2_6451']/tbody/tr"):
            print("statistiques2_6451", tag, tag.text)
     
        print("-"*12)
        print("Autre façon de parvourir...")
        for id in ("statistiques_6451", "statistiques2_6451"):
            for tag in driver.find_elements(By.CSS_SELECTOR, f"table#{id} tr"):
                print(id, tag, tag.text)
     
    finally:
        driver.quit()

  11. #11
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    J'ai essayé cela :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    from pathlib import Path
    import sys
    import os
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    #from selenium.webdriver.support.ui import Select
    # from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
     
     
    CONTENT = """<html>
     
            <div id="statistiques_6451_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer"><div class="row btable-row1"><div id="btable_statistiques_6451_caption" class="col-sm-8"></div><div id="btable_statistiques_6451_length" class="col-sm-4"></div></div><div class="row btable-row2"><div id="btable_statistiques_6451_filter" class="col-sm-6"><div id="statistiques_6451_filter" class="dataTables_filter" style="float: left;"><label>Rechercher&nbsp;:<input type="search" class="form-control input-sm" placeholder="" aria-controls="statistiques_6451" style="margin: 0px 10px 0px 0px; background-color: rgb(255, 255, 255); border-color: rgb(204, 204, 204); border-width: 1px;"></label></div><div class="dt-buttons btn-group"></div><div class="btn-group" id="statistiques_6451_btn_export" role="group"><button title="Exporter le tableau" type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">Export</button><div id="statistiques_6451_export" class="dropdown-menu dropdown-export"><a class="btn btn-default buttons-csv buttons-html5 btn-sm btn-export-format" tabindex="0" aria-controls="statistiques_6451" href="#"><span><i class="fa fa-file-text-o" aria-hidden="true" style="font-size:16px"></i> CSV</span></a><a class="btn btn-default buttons-excel buttons-html5 btn-sm btn-export-format" tabindex="0" aria-controls="statistiques_6451" href="#"><span><i class="fa fa-file-excel-o" aria-hidden="true" style="font-size:16px"></i> Excel</span></a><a class="btn btn-default buttons-print btn-sm btn-export-format" tabindex="0" aria-controls="statistiques_6451" href="#"><span><i class="fa fa-print" aria-hidden="true" style="font-size:16px"></i> Impression</span></a></div></div><button title="Reinitialiser le tableau" class="btn btn-default btn-sm" id="statistiques_6451_btn_reinitialiser"><span class="glyphicon glyphicon-repeat"></span> Reinitialiser</button></div><div id="btable_statistiques_6451_pagination" class="col-sm-6"></div></div><div class="row btable-row3"><div id="btable_statistiques_6451_table" class="col-sm-12"><table class="btable table table-bordered table-condensed table-striped table-hover no-footer dataTable" id="statistiques_6451" data-table-pagination="false" data-table-export="true" data-table-page-info="false" data-table-fixheader="false" data-table-export-nom="Total des types de bac actuels par voeux" data-table-export-format="csv,excel,print" role="grid" style="visibility: visible;">
                    <thead>
                    </thead>
                    <tbody>
     
                    <tr class="odd" role="row">
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                    </tr><tr class="even" role="row">
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                    </tr><tr class="odd" role="row">
                                            <td class=" text-left">6</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">6</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">6</td>
                                    </tr><tr class="even" role="row">
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                    </tr><tr class="odd" role="row">
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
     
                                    </tr></tbody>
            </table><div id="statistiques_6451_processing" class="dataTables_processing panel panel-default" style="display: none;">Traitement en cours...</div></div></div><div class="row btable-row4"><div id="btable_statistiques_6451_info" class="col-sm-5"></div><div id="btable_statistiques_6451_pagination_bottom" class="col-sm-7"></div></div></div>
            <br>
            
            <h2 class="mt-1 mb-1 ml-1">équivalents</h2>
            <div id="statistiques2_6451_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer"><div class="row btable-row1"><div id="btable_statistiques2_6451_caption" class="col-sm-8"></div><div id="btable_statistiques2_6451_length" class="col-sm-4"></div></div><div class="row btable-row2"><div id="btable_statistiques2_6451_filter" class="col-sm-6"><div id="statistiques2_6451_filter" class="dataTables_filter" style="float: left;"><label>Rechercher&nbsp;:<input type="search" class="form-control input-sm" placeholder="" aria-controls="statistiques2_6451" style="margin: 0px 10px 0px 0px; background-color: rgb(255, 255, 255); border-color: rgb(204, 204, 204); border-width: 1px;"></label></div><div class="dt-buttons btn-group"></div><div class="btn-group" id="statistiques2_6451_btn_export" role="group"><button title="Exporter le tableau" type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">Export</button><div id="statistiques2_6451_export" class="dropdown-menu dropdown-export"><a class="btn btn-default buttons-csv buttons-html5 btn-sm btn-export-format" tabindex="0" aria-controls="statistiques2_6451" href="#"><span><i class="fa fa-file-text-o" aria-hidden="true" style="font-size:16px"></i> CSV</span></a><a class="btn btn-default buttons-excel buttons-html5 btn-sm btn-export-format" tabindex="0" aria-controls="statistiques2_6451" href="#"><span><i class="fa fa-file-excel-o" aria-hidden="true" style="font-size:16px"></i> Excel</span></a><a class="btn btn-default buttons-print btn-sm btn-export-format" tabindex="0" aria-controls="statistiques2_6451" href="#"><span><i class="fa fa-print" aria-hidden="true" style="font-size:16px"></i> Impression</span></a></div></div><button title="Reinitialiser le tableau" class="btn btn-default btn-sm" id="statistiques2_6451_btn_reinitialiser"><span class="glyphicon glyphicon-repeat"></span> Reinitialiser</button></div><div id="btable_statistiques2_6451_pagination" class="col-sm-6"></div></div><div class="row btable-row3"><div id="btable_statistiques2_6451_table" class="col-sm-12"><table class="btable table table-bordered table-condensed table-striped table-hover no-footer dataTable" id="statistiques2_6451" data-table-pagination="false" data-table-export="true" data-table-page-info="false" data-table-fixheader="false" data-table-export-nom="Total des types de bac par voeux" data-table-export-format="csv,excel,print" style="visibility: visible;" role="grid">
                    <thead>
                    </thead>
                    <tbody>
     
                    <tr role="row" class="odd">
                                            <td class=" text-left">11</td>
                                            <td class=" text-left">2</td>
                                            <td class=" text-left">9</td>
                                            <td class=" text-left">6</td>
                                            <td class=" text-left">5</td>
                                    </tr><tr role="row" class="even">
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                            <td class=" text-left">0</td>
                                            <td class=" text-left">1</td>
                                    </tr></tbody>
            </table><div id="statistiques2_6451_processing" class="dataTables_processing panel panel-default" style="display: none;">Traitement en cours...</div></div></div><div class="row btable-row4"><div id="btable_statistiques2_6451_info" class="col-sm-5"></div><div id="btable_statistiques2_6451_pagination_bottom" class="col-sm-7"></div></div></div>
            <br>
            
            <div id="bouton">
                    <table class="bouton">
            <tbody><tr>
                    <td class="gauche"></td>
                    <td class="milieu"><a href="statistiques?ACTION=1" class="bouton">Retour</a>            </td>
                    <td class="droite"></td>
            </tr></tbody></table>
     
            </div>
     
     
    </html>
    """
    FILE_ = Path.home() / "toto.html"
    FILE_.write_text(CONTENT)
    print(FILE_)
     
     
    try:
        driver.get(f"file://{FILE_}")
        print(driver.page_source)
     
        for tag in driver.find_elements(By.XPATH, "//table[@id='statistiques_6451']/tbody/tr"):
            print("statistiques_6451", tag, tag.text)
        for tag in driver.find_elements(By.XPATH, "//table[@id='statistiques2_6451']/tbody/tr"):
            print("statistiques2_6451", tag, tag.text)
     
        print("-"*12)
        print("Autre façon de parcourir...")
        for id in ("statistiques_6451", "statistiques2_6451"):
            for tag in driver.find_elements(By.CSS_SELECTOR, f"table#{id} tr"):
                print(id, tag, tag.text)
     
    finally:
        driver.quit()
    Cela semble être ok ici.
    Ce que je ne comprends pas, c'est que das le html, je ne retrouve pas ta table telle qu'elle apparaît sur la capture d'écran :
    Nom : Capture d’écran du 2024-01-28 17-04-14.png
Affichages : 217
Taille : 10,5 Ko

    Citation Envoyé par jurassic pork Voir le message
    OK
    dans le code de ton premier message est-ce que tab_details_ciel2 possède une valeur après affectation et que fais tu après dans ton code. Et je vois dans ton code HTML qu'il y a des traitements en cours. Attends tu que les traitements soient terminés avant de capturer les tables ?
    tab_details_ciel2 possède bien une valeur mais la liste est vide. Pour ce qui est des traitements, je ne vois pas ce qui serait bloquant.
    Les tables sont capturées indépendamment l'une de l'autre. J'ai même tenté d'inverser ou de supprimer la première, rien à faire.
    Je pense que c'est un truc tout bête qui m'échappe. Cela va venir...

  12. #12
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    Pas encore testé, mais ta méthode, papajoker, va peut-être simplifier mes affaires.
    En attendant, je dois fatiguer car avec la deuxième séquence, cela plante, je ne comprends pas pourquoi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    print("Autre façon de parcourir...")
    for id in ("statistiques_6451", "statistiques2_6451"):
        for tag in driver.find_elements(By.CSS_SELECTOR, f"table#{id} tr"):
            print(id, tag, tag.text)
     
    tab_details_ciel2 = []
    for id in ("statistiques2_6451"):
        for tag in driver.find_elements(By.CSS_SELECTOR, f"table#{id} tr"):
            print(id, tag, tag.text)
            tab_details_ciel2.append(tag.text)
    print("tab_details_ciel2 : ", tab_details_ciel2)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
        for tag in driver.find_elements(By.CSS_SELECTOR, f"table#{id} tr"):
     
      File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 1284, in find_elements
        return self.execute(Command.FIND_ELEMENTS, {
     
      File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 430, in execute
        self.error_handler.check_response(response)
     
      File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
        raise exception_class(message, screen, stacktrace)
     
    InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
      (Session info: chrome=121.0.6167.85)

  13. #13
    Expert confirmé Avatar de papajoker
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2013
    Messages
    2 322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2013
    Messages : 2 322
    Par défaut
    ta version de selenium est plus ancienne que moi, pas grave si CSS_SELECTOR n'est pas valide

    Sinon maintenant, avec ton code html, je peux voir qu'en fait, tu récupères des tableaux qui ont un attribut role=grid
    Autant l'utiliser pour avoir un code plus générique :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from collections import defaultdict
    ...
    tables = defaultdict(list)
    try:
        driver.get(f"file://{FILE_}")
        for table in driver.find_elements(By.XPATH, "//table[@role='grid']"):
            for tag in table.find_elements(By.XPATH, "//tr[@role='row']"):
                tables[table.get_attribute('id')].append(tuple(tag.text.split()))
    finally:
        driver.quit()
     
    """
    maintenant toutes les datas sont dans le dico "tables", 
    on affiche et/ou sauvegarde tout ou uniquement les "id" recherchés
    """
    for key, values in tables.items():
        print(key, values)

  14. #14
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    Merci à vous ! Tout est ok.
    Une idée de la raison pour laquelle mon code initial ne fonctionne pas ?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. trouver le nom de la table associée à un trigger
    Par ndufour dans le forum Développement
    Réponses: 2
    Dernier message: 09/02/2006, 16h25
  2. [Débutant][indexes]Trouver les indexes d'une table
    Par Gromitou dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 13/12/2005, 17h50
  3. trouver et remplacer dans une table paradox
    Par Yepazix dans le forum Bases de données
    Réponses: 3
    Dernier message: 23/09/2004, 02h19
  4. [Bdd ]Trouver la clé primaire d'une table
    Par drosof dans le forum JDBC
    Réponses: 3
    Dernier message: 11/08/2004, 10h58
  5. Trouver le dernier enregistrement d'une table
    Par remi59 dans le forum Requêtes
    Réponses: 4
    Dernier message: 11/03/2003, 14h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo