Selenium cannot get all elements of a page


i am using selenium to go search on agoda and scrape all the hotel name in the page, but the output only return 2 names.

Then i tried to add a line to scroll to the bottom, now the output gives me first 2 names and last 2 names (first two from beginning, last two from bottom)

I don’t understand what’s the problem, i added time.sleep() for each step so the whole page should have been loaded completely. Does selenium limit by page view that it can only scrape those element in sight?

my code below:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")


def scrape():
    r = requests.get(current_page)

if r.status_code ==
    print('start scraping!')
    hotel = driver.find_elements_by_class_name('hotel-name')
    hotels = []
    for h in hotel:
        if hotel:
    print(hotels, file=open("output.txt", 'a', encoding="utf-8"))

Here is the page i want to scrape


Try to use below script to scroll page down until no more results appeared on page and then scrape all available names:

from selenium import webdriver
from import WebDriverWait as wait
from import By
from import expected_conditions as EC

driver = webdriver.Chrome()

# Get initial list of names
hotels = wait(driver, 15).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'hotel-name')))

while True:
    # Scroll down to last name in list
    driver.execute_script('arguments[0].scrollIntoView();', hotels[-1])
        # Wait for more names to be loaded
        wait(driver, 15).until(lambda driver: len(wait(driver, 15).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'hotel-name')))) > len(hotels))
        # Update names list 
        hotels = wait(driver, 15).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'hotel-name')))
        # Break the loop in case no new names loaded after page scrolled down

# Print names list
print([hotel.text for hotel in hotels])

Answered By – Andersson

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published