Home > database >  Scroll down at the bottom of a webpage (selenium/python)
Scroll down at the bottom of a webpage (selenium/python)

Time:01-04

I am trying to get all the images from this webpage: "https://www.airbnb.com/rooms/43871809/photos?guests=1&adults=1"

I am using XPath to get all the images but if I don't scroll down at the bottom then XPath only gets 13 images when it should get 39. I am using the following code:

   s = Service('D:\Selenium driver\chromedriver2.exe')
   driver = webdriver.Chrome(service=s)
   url = 'https://www.airbnb.com/rooms/43871809/photos?guests=1&adults=1'
   driver.get(url)
   time.sleep(4)
   driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
   images = driver.find_elements_by_xpath('//div[@]/div/div/picture/img')

I am other methods to create scroll action. But I think the problem lies with the page. Can anyone provide me a solution with the scrolling or any other method getting all the 39 images.

P.S: I am new at this and still learning and I appreciate your help. Thanks

CodePudding user response:

I tried with below xpath and it returned 39 as the count of the images.

//div[@data-testid='photo-viewer-section']//a

The code:

#Imports Required:

from selenium.webdriver.common.by import By

driver.get("https://www.airbnb.com/rooms/43871809/photos?guests=1&adults=1")

images_count = driver.find_elements(By.XPATH,"//div[@data-testid='photo-viewer-section']//a")

print(len(images_count))

Output:

39
  •  Tags:  
  • Related