英文:
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
执行步骤:
-
首先,等待滚动条网页元素在页面上可见/加载,并将其分配给变量
scroll_bar
。 -
接下来,获取此
scroll_bar
的当前高度,并将其分配给变量last_height
。 -
开始循环,在每个迭代中,向下滚动滚动条底部,暂停一下,获取滚动条的高度,并将其分配给变量
new_height
,然后检查new_height == last_height
,如果为真,跳出循环(flag=Flase
),否则,更新变量last_height
为new_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:
-
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
-
Next, we get the current height of this
scroll_bar
and assign it to a variablelast_height
. -
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 thenew_height == last_height
, break out of the loop(flag=Flase
) otherwise, update the variablelast_height
withnew_height
and repeat this step until theif condition
is True.
I hope this solves your problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论