1

This is with reference to this question. Trying to scrape this site

Expected output is :

Name of the Hotel

First arrangement option : its prize

Code I have tried so far:

driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://tn.tunisiebooking.com/")
#code to choose the option "Sousse" ,date- 06/08/21, and click on "Rechercher".

hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for hotel in hotels:
    name = hotel.find_element_by_tag_name("h3").text
    argmts = hotel.find_element_by_class_name("angle_active").text
    prize = hotel.find_element_by_xpath("//div[starts-with(@id,'prixtotal_')]").text
    print(name)
    print(argmts + ':' + prize)
driver.quit()

The output I get:

Petit dejeuner:60
Tui Blue Scheherazade
Demi pension:60
Golf Residence GAS
Petit dejeuner:60
Sindbad Center GAS
Logement seul:60 ...

Applied scrolling effect and time.sleep. But the prize remains same, its doesn't get the prize of the respective Hotel. Not sure where its going wrong.

pmadhu
  • 3,373
  • 2
  • 11
  • 23

2 Answers2

3

This has worked for me, with the .get_attribute

driver.get("https://tn.tunisiebooking.com/")
select = driver.find_element_by_xpath("//select[@id='ville_des']")
option = Select(select)

option.select_by_index(3)
#code to choose the option "Sousse" ,date- 06/08/21, and click on "Rechercher".
search = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"((//div[@class='col-lg-5 col-md-5 col-sm-5'])[1])")))
search.click()
hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for hotel in hotels:
name = hotel.find_element_by_tag_name("h3").text

argmts = hotel.find_element_by_class_name("angle_active").text

#taken the help of contains() and tried to get the static part of the element ID.
prize = hotel.find_element_by_xpath(".//div[contains(@id,'prixtotal_')]").get_attribute("innerText")
print(name)
print(argmts + ':' + prize)
driver.quit()

O/P

https://i.stack.imgur.com/FdYJX.png

YaDav MaNish
  • 1,260
  • 2
  • 12
  • 20
2

I would get each hotel element separately, move to it and then get inner elements details.
Also, the best way to get elements inside element is with XPath staring with dot . as below.
Try this:

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)


hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for i in range (1, len(hotels)+1):
    hotel = driver.find_element_by_xpath("(//div[starts-with(@id,'produit_affair')])[" + str(i) + "]")
    actions.move_to_element(hotel).perform()
    time.sleep(0.5)
    name = hotel.find_element_by_xpath(".//h3").text
    argmts = hotel.find_element_by_xpath(".//div[@class='angle_active']").text
    prize = hotel.find_element_by_xpath(".//div[starts-with(@id,'prixtotal_')]").text
    print(name)
    print(argmts + ':' + prize)
driver.quit()
Prophet
  • 32,350
  • 22
  • 54
  • 79