如何使用Selenium和Python滚动到内部滚动条的底部?

huangapple go评论62阅读模式
英文:

How to scroll down to the bottom of an inner scroll bar using Selenium with Python?

问题

我正在尝试使左侧的“详细信息面板”的自动滚动器工作,但似乎无法使其工作。我也尝试查找iframes,但找不到。

这是我的代码:

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://kart.1881.no/?query=1010')
time.sleep(8)

driver.find_element(By.CLASS_NAME, 'main-panel-scroll')

SCROLL_PAUSE_TIME = 0.5

while True:
    last_height = driver.execute_script("return document.body.scrollHeight")
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(SCROLL_PAUSE_TIME)
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        time.sleep(SCROLL_PAUSE_TIME)
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        else:
            last_height = new_height
            continue

time.sleep(10)

我已尝试使用不同的元素以不同的方式(如xpath、类名等)来使滚动器“关注”左侧面板,但都没有成功。代码没有报错。

看起来也不像它使用iframes。

英文:

I'm trying to make the auto scroller work for the "details panel" on the left side of this website but I can't seem to make it work. I've tried looking for iframes as well but I can't find any.

Here is my code:

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://kart.1881.no/?query=1010')
time.sleep(8)

driver.find_element(By.CLASS_NAME, 'main-panel-scroll')

SCROLL_PAUSE_TIME = 0.5

while True:
    last_height = driver.execute_script("return document.body.scrollHeight")
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(SCROLL_PAUSE_TIME)
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
            time.sleep(SCROLL_PAUSE_TIME)
            new_height = driver.execute_script("return document.body.scrollHeight")
            if new_height == last_height:
                break
            else:
                last_height = new_height
                continue


time.sleep(10) 

I have tried to use different elements to make the scroller "focus" on the left panel using various methods like xpath, class name, etc but none of them allows me to scroll. The code closes out without errors.

It doesn't look like it uses iframes either.

答案1

得分: 0

以下是完整的解决方案:

import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC

driver = Chrome()
wait = WebDriverWait(driver, 10)
driver.get('https://kart.1881.no/?query=1010')

scroll_bar = wait.until(EC.visibility_of_element_located((By.ID, 'search_result')))

flag = True
last_height = driver.execute_script("return arguments[0].scrollHeight", scroll_bar)
SCROLL_PAUSE_TIME = 0.5

while flag:
    driver.execute_script("arguments[0].scrollBy(0, arguments[0].scrollHeight);", scroll_bar)
    time.sleep(SCROLL_PAUSE_TIME)

    new_height = driver.execute_script("return arguments[0].scrollHeight", scroll_bar)

    if new_height == last_height:
        flag = False
    else:
        last_height = new_height

执行步骤:

  1. 首先,等待滚动条网页元素在页面上可见/加载,并将其分配给变量 scroll_bar

  2. 接下来,获取此 scroll_bar 的当前高度,并将其分配给变量 last_height

  3. 开始循环,在每个迭代中,向下滚动滚动条底部,暂停一下,获取滚动条的高度,并将其分配给变量 new_height,然后检查 new_height == last_height,如果为真,跳出循环(flag=Flase),否则,更新变量 last_heightnew_height,并重复此步骤,直到 if condition 为真。

希望这解决了您的问题。

英文:

Here's the complete solution:

import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC

driver = Chrome()
wait = WebDriverWait(driver, 10)
driver.get('https://kart.1881.no/?query=1010')

scroll_bar = wait.until(EC.visibility_of_element_located((By.ID, 'search_result')))

flag = True
last_height = driver.execute_script("return arguments[0].scrollHeight", scroll_bar)
SCROLL_PAUSE_TIME = 0.5

while flag:
    driver.execute_script("arguments[0].scrollBy(0, arguments[0].scrollHeight);", scroll_bar)
    time.sleep(SCROLL_PAUSE_TIME)

    new_height = driver.execute_script("return arguments[0].scrollHeight", scroll_bar)

    if new_height == last_height:
        flag = False
    else:
        last_height = new_height

steps followed:

  1. First, we wait for the scroll-bar web element to get visibly located/loaded on the page and assign it to a variable scroll_bar

  2. Next, we get the current height of this scroll_bar and assign it to a variable last_height.

  3. start looping and in each iteration, scroll down to the bottom of the scroll bar, take a pause, get the height of the scroll bar, and assign it to a variable new_height and check if the new_height == last_height, break out of the loop(flag=Flase) otherwise, update the variable last_height with new_height and repeat this step until the if condition is True.

I hope this solves your problem.

huangapple
  • 本文由 发表于 2023年6月19日 10:13:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定