【问题标题】:How to scroll to the end Selenium Python如何滚动到最后 Selenium Python
【发布时间】:2021-12-11 07:20:31
【问题描述】:

我正在尝试滚动到此页面的末尾url

进入页面后,我单击“显示所有 77 种产品”按钮,我进入了一个弹出窗口,其中部分元素显示在弹出窗口中。实际上这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

def getpage(driver):
    driver.get('https://www.binance.com/it/pos')
    sleep(3)
    driver.find_element_by_xpath('//div[@id="savings-lending-pos-expend"]').click()
    sleep(2)
    elem = driver.find_element_by_xpath('//div[@class="css-n1ers"]')
    elem.send_keys(Keys.END)

driver = webdriver.Firefox()
getpage(driver)

我已经尝试了几乎所有的工作,除了上面代码中的解决方案,我尝试了 nu 成功当前的解决方案:

driver.execute_script("window.scrollTo(0, Y)") 

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

在这个解决方案中,我不明白要使用哪个标签

label.sendKeys(Keys.PAGE_DOWN);

我尝试了几乎所有的解决方案,但都没有奏效。我希望你能帮助我。谢谢。

【问题讨论】:

    标签: selenium web-scraping selenium-chromedriver webautomation


    【解决方案1】:

    尝试如下并确认:

    您可以尝试找到每一行并申请scrollIntoView

    # Imports required for Explicit wait:
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    def getpage(driver):
        driver.get('https://www.binance.com/it/pos')
        wait = WebDriverWait(driver,30)
        wait.until(EC.element_to_be_clickable((By.ID,"savings-lending-pos-expend"))).click() // show more option
        i = 0
        try:
            while True:
                options = driver.find_elements_by_xpath("//div[@id='modal-wrapper']/div") // Try to find rows.
                driver.execute_script("arguments[0].scrollIntoView(true);",options[i])
                time.sleep(1)
                i+=1
        except IndexError as e:
            print("Exception: {}".format(e.args[-1]))
        print(i)
    
    getpage(driver)
    

    【讨论】:

    • 返回 0 但不滚动。
    • @giacomomaraglino - 这是因为行的 class-name 已更改。将 xpath 更新为 //div[@id='modal-wrapper']/div 并尝试。
    • 我找到了解决方案。如果它适合你!
    【解决方案2】:

    您可以使用ActionChains

    from selenium.webdriver.common.action_chains import ActionChains
    
    actions = ActionChains(driver)
    actions.send_keys(Keys.PAGE_DOWN).perform()
    

    这将使页面向下滚动,类似于按Page Down 键。

    【讨论】:

    • 我很喜欢它。编译器没有给出错误,但它不会滚动。
    • 我忘了添加.perform()。我修改了我的答案。试试看,让我们知道
    • 我找到了解决方案。如果它适合你!
    【解决方案3】:

    这个解决方案奏效了:

    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from time import sleep
    
    def getpage(driver):
        driver.get('https://www.binance.com/it/pos')
        wait = WebDriverWait(driver,30)
        wait.until(EC.element_to_be_clickable((By.ID,"savings-lending-pos-expend"))).click() 
        i = 0
        sleep(5)
        pop_up_window = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='modal-scroller']")))
    
        while True:
            driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', pop_up_window)
            sleep(1)
    
    driver = webdriver.Firefox()
    getpage(driver)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多