how to extract data from an element list python

Question:

I am working on a project that really blocked me I often asked questions here and you have helped me a lot since I am still a beginner, my project consists in making a competitive watch table for hotel rates for an agency It is a painful action that I wanted to automate it, I succeeded in extracting the tariffs and their prices, but the problem is that I want him to give me only the selected room
I provide you with the code and the output i removed the data that i want to elimnate in my output also i’ve addede images to better clarify things if any of you can help me and thank you in advance.

NB : thanks to pmadhu’s answer problem solved but now it shows me the same rates for all hotels.

enter image description here

#!/usr/bin/env python
# coding: utf-8
import json
import time
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.common.exceptions import StaleElementReferenceException

# create path and start webdriver
PATH = "C:chromedriver.exe"
driver = webdriver.Chrome(PATH)

# first get website
driver.get('https://tn.tunisiebooking.com/')
wait = WebDriverWait(driver, 20)

# params to select
params = {
    'destination': 'Nabeul',
    'date_from': '24/08/2021',
    'date_to': '25/08/2021',
    'bedroom': '1'
}

# select destination
destination_select = Select(driver.find_element_by_id('ville_des'))
destination_select.select_by_value(params['destination'])

# select bedroom
bedroom_select = Select(driver.find_element_by_id('select_ch'))
bedroom_select.select_by_value(params['bedroom'])

# select dates
script = f"document.getElementById('depart').value ='{params['date_from']}';"
script += f"document.getElementById('checkin').value ='{params['date_to']}';"
driver.execute_script(script)

# submit form
btn_rechercher = driver.find_element_by_id('boutonr')
btn_rechercher.click()
sleep(10)

# ----------------------------------------------------------------------------
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import StaleElementReferenceException,NoSuchElementException
urls = []
hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for hotel in hotels:
    link = hotel.find_element_by_xpath(".//span[@class='tittre_hotel']/a").get_attribute("href")
    urls.append(link)
for url in urls:
    driver.get(url)
    try:
        name = driver.find_element_by_xpath("//div[@class='bloc_titre_hotels']/h2").text
        arropt = driver.find_element_by_xpath("//div[contains(@class,'line_result')][1]")
        opt = arropt.find_element_by_tag_name("b").text
        num = len(arropt.find_elements_by_tag_name("option"))
        optiondata = {}
        achats = {}
        marges= {}
        selection = Select(driver.find_element_by_id("arrangement"))
        for i in range(num):
            try:
                selection = Select(driver.find_element_by_id("arrangement"))
                selection.select_by_index(i)
                time.sleep(2)
                arr = driver.find_element_by_xpath("//select[@id='arrangement']/option[@selected='selected']").text
                prize = driver.find_element_by_id("prix_total").text
                
                optiondata[arr]=prize

                
                btn_passe = driver.find_element_by_xpath('//*[@id="resultat"]/div/form/div/div[2]/div[1]/div[2]/div[2]/div/div ')
                btn_passe.click()
                sleep(2)
                                    # params to select
                params = {
                            'civilite_acheteur': 'Mlle',
                            'prenom_acheteur': 'test',
                            'nom_acheteur': 'test',
                            'e_mail_acheteur': '[email protected]',
                            'portable_acheteur': '22222222'
                        }

                        # select civilite
                civilite_acheteur = Select(driver.find_element_by_id('civilite_acheteur'))
                civilite_acheteur.select_by_value(params['civilite_acheteur'])

                        # saisir prenom 
                script = f"document.getElementById('prenom_acheteur').value ='{params['prenom_acheteur']}';"
                script += f"document.getElementById('nom_acheteur').value ='{params['nom_acheteur']}';"
                script += f"document.getElementById('e_mail_acheteur').value ='{params['e_mail_acheteur']}';"
                script += f"document.getElementById('portable_acheteur').value ='{params['portable_acheteur']}';"
                driver.execute_script(script)

                        # submit form
                btn_rechercher = driver.find_element_by_id('titre_Hammamet')
                btn_rechercher.click()
                sleep(2)

                btn_rechercher = driver.find_element_by_id('boutonr')
                btn_rechercher.click()
                sleep(3)
                
                achat = driver.find_element_by_xpath('/html/body/header/div[2]/div[1]/div[1]/div[4]/div[2]/div[2]').text.replace(' TND', '')
                achats[arr]=achat

                marge =int(((float(prize) - float(achat)) / float(achat)) * 100);

                marges[arr]=marge
                optiondata[arr]=prize,achat,marge
                
                driver.get(url)
                btn_passe = driver.find_element_by_xpath('//*[@id="moteur_rech"]/form/div/div[3]/div')
                btn_passe.click()
                sleep(2)
                

            except StaleElementReferenceException:
                pass
            
       
    except NoSuchElementException:
        pass
    print("{} : {} - {}".format(name,opt,optiondata))
    

enter image description here

enter image description here

enter image description here

Asked By: HiFAR

||

Answers:

Try below code once:

from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import StaleElementReferenceException,NoSuchElementException

hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for hotel in hotels:
    link = hotel.find_element_by_xpath(".//span[@class='tittre_hotel']/a").get_attribute("href")
    urls.append(link)
for url in urls:
    driver.get(url)
    try:
        name = driver.find_element_by_xpath("//div[@class='bloc_titre_hotels']/h2").text
        arropt = driver.find_element_by_xpath("//div[contains(@class,'line_result')][1]")
        opt = arropt.find_element_by_tag_name("b").text
        num = len(arropt.find_elements_by_tag_name("option"))
        optiondata = {}
        selection = Select(driver.find_element_by_id("arrangement"))
        for i in range(num):
            try:
                selection = Select(driver.find_element_by_id("arrangement"))
                selection.select_by_index(i)
                time.sleep(2)
                arr = driver.find_element_by_xpath("//select[@id='arrangement']/option[@selected='selected']").text
                prize = driver.find_element_by_id("prix_total").text
                optiondata[arr]=prize
            except StaleElementReferenceException:
                pass
    except NoSuchElementException:
        pass
    print("{} : {} - {} - {}".format(name,opt,num,optiondata))

And the output:

Tui Blue Scheherazade Sousse : Double Standard Vue Mer - 1 - {'Demi Pension': '114'}
Golf Residence GAS Sousse : Double--Standard - 2 - {'Demi Pension': '51', 'Petit Dejeuner': '42'}
Sindbad Center GAS Sousse : Chambre Double - 2 - {'Petit Dejeuner': '27', 'Logement seul': '22'}
Answered By: pmadhu