Condition to check if Selenium is done scrolling based on web element?

℡╲_俬逩灬. 提交于 2021-02-17 01:54:08

问题


Currently I have a script that will go to TripAdvisor and try to scrape every image in that particular filter. I was wondering what conditional I should set my if statement to in order for it to break out of the while loop and then parse the list of urls to give me clear url links to each image. I am just confused at how I can tell if I have reached the end once I have reached the last web element. The if statement is right at the end before the last printing loop. Any help is greatly appreciated!

# import dependencies
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import re
import selenium
import io
import pandas as pd
import urllib.request
import urllib.parse
import requests
from bs4 import BeautifulSoup
import pandas as pd
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time
from _datetime import datetime
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.headless=False

driver = webdriver.Chrome("/Users/rishi/Downloads/chromedriver 3")
driver.maximize_window()
prefs = {"profile.default_content_setting_values.notifications" : 2} 
options.add_experimental_option("prefs", prefs)

#open up website
driver.get(
    "https://www.tripadvisor.com/Hotel_Review-g28970-d84078-Reviews-Hyatt_Regency_Washington_on_Capitol_Hill-Washington_DC_District_of_Columbia.html#/media/84078/?albumid=101&type=2&category=101")

image_url = []

end = False
while not(end):
    #wait until element is found and then store all webelements into list
    images = WebDriverWait(driver, 20).until(
        EC.presence_of_all_elements_located(
            (By.XPATH, '//*[@class="media-viewer-dt-root-GalleryImageWithOverlay__galleryImage--1Drp0"]')))

    #iterate through visible images and acquire their url based on background image style
    for index, image in enumerate(images):
        image_url.append(images[index].value_of_css_property("background-image"))

    #if you are at the end of the page then leave loop
    # if(length == end_length):
    #     end = True

    #move to next visible images in the array
    driver.execute_script("arguments[0].scrollIntoView();", images[-1])

    #wait one second
    time.sleep(1)

    if():
        end = True

#clean the list to provide clear links
for i in range(len(image_url)):
    start = image_url[i].find("url(\"") + len("url(\"")
    end = image_url[i].find("\")")
    print(image_url[i][start:end]) 

#print(image_url)

来源:https://stackoverflow.com/questions/60321995/condition-to-check-if-selenium-is-done-scrolling-based-on-web-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!