python 如何将页面向下滚动?

xlpyo6sf  于 2023-08-02  发布在  Python
关注(0)|答案(1)|浏览(153)

我去一个推文的网址,它有很多评论在那里!这是我尝试过的两段代码。
使用代码1,它向下滚动到页面的末尾,但因为下拉太大,找不到元素。
使用代码2,我将其更改为缓慢向下滚动1000,但它只向下滚动了几次,然后停止,我检查了一下,Python说它已经向下滚动了页面。我试着改变参数,但它只滚动了几次。
代码1:

`while True:
                last_height = driver.execute_script("return document.body.scrollHeight")
                driver.execute_script("window.scrollBy(0, document.body.scrollHeight);")
                time.sleep(2)
                new_height = driver.execute_script("return document.body.scrollHeight")
                try:
                    elements = driver.find_elements(By.XPATH,
                                                    "//a[@class='css-4rbku5 css-18t94o4 css-901oao r-14j79pv r-1loqt21 r-xoduu5 r-1q142lx r-1w6e6rj r-37j5jr r-a023e6 r-16dba41 r-9aw3ui r-rjixqe r-bcqeeo r-3s2u2q r-qvutc0']")                   
                    click4 = driver.find_element(By.XPATH,
                                                 "//div[@class='css-1dbjc4n r-16y2uox r-1wbh5a2 r-1777fci']")
                    click4.click()
                except:
                    time.sleep(0.5)
                try:
                    elements = driver.find_elements(By.XPATH,
                                                    "//a[@class='css-4rbku5 css-18t94o4 css-901oao r-14j79pv r-1loqt21 r-xoduu5 r-1q142lx r-1w6e6rj r-37j5jr r-a023e6 r-16dba41 r-9aw3ui r-rjixqe r-bcqeeo r-3s2u2q r-qvutc0']")
                    click5 = driver.find_element(By.XPATH, "//div[@class='css-1dbjc4n r-1ndi9ce']")
                    click5.click()
                except:
                    time.sleep(0.5)

                if new_height == last_height:
                    print("end")
                    time.sleep(10)  
                    break`

字符串
代码2:

while True:
                last_height = driver.execute_script("return document.body.scrollHeight")
                driver.execute_script("window.scrollBy(0, 1000)", "")
                time.sleep(2)
                new_height = driver.execute_script("return document.body.scrollHeight")
                try:
                    elements = driver.find_elements(By.XPATH,
                                                    "//a[@class='css-4rbku5 css-18t94o4 css-901oao r-14j79pv r-1loqt21 r-xoduu5 r-1q142lx r-1w6e6rj r-37j5jr r-a023e6 r-16dba41 r-9aw3ui r-rjixqe r-bcqeeo r-3s2u2q r-qvutc0']")                   
                    click4 = driver.find_element(By.XPATH,
                                                 "//div[@class='css-1dbjc4n r-16y2uox r-1wbh5a2 r-1777fci']")
                    click4.click()
                except:
                    time.sleep(0.5)
                try:
                    elements = driver.find_elements(By.XPATH,
                                                    "//a[@class='css-4rbku5 css-18t94o4 css-901oao r-14j79pv r-1loqt21 r-xoduu5 r-1q142lx r-1w6e6rj r-37j5jr r-a023e6 r-16dba41 r-9aw3ui r-rjixqe r-bcqeeo r-3s2u2q r-qvutc0']")
                    click5 = driver.find_element(By.XPATH, "//div[@class='css-1dbjc4n r-1ndi9ce']")
                    click5.click()
                except:
                    time.sleep(0.5)
                if new_height == last_height:
                    print("end")
                    time.sleep(10)  
                    break


我试了很多次,但它不工作,希望有人能帮助我找到问题。谢啦,谢啦

gz5pxeao

gz5pxeao1#

如果你真的想在前端做这件事,我有解决方案。

wdwait = WebDriverWait(driver, 10)
last_height = driver.execute_script("return window.pageYOffset")
driver.execute_script("window.scrollBy(0, 1000)", "")
driver.execute_script("function waitForValueNotToStopChanging(valueGetter, interval) {return new Promise((resolve) => {let previousValue = valueGetter();let unchangedCount = 0;const intervalId = setInterval(() => {const currentValue = valueGetter();if (currentValue === previousValue) {unchangedCount++;} else {unchangedCount = 0;}if (unchangedCount >= 2) {clearInterval(intervalId);resolve(currentValue);}previousValue = currentValue;}, interval);});}let scrollHeightGetter = () => document.body.scrollHeight; await waitForValueNotToStopChanging(scrollHeightGetter, 500);")
wdwait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "[role=progressbar]")))
new_height = driver.execute_script("return window.pageYOffset")

字符串
代码2中应该在while之后和try之前使用这些行。
1.首先,我将条件改为window.pageYOffset,因为它更稳定(在任何移动后直接更改)。
1.除了隐式等待,我检查scrollHeight在2个时间帧(1秒或更长时间)内没有改变,所以你可以确定滚动发生了。
1.我等待新的推文加载器是不可见的(它可以出现后滚动+稳定的位置)
1.只有这样我才能再次得到window.pageYOffset
我测试了你提供的推文,并成功地滚动到底部。
下面是我使用的js函数(但您可以将条件从document.body.scrollHeight更改为window.pageYOffset,例如

function waitForValueNotToStopChanging(valueGetter, interval) {
  return new Promise((resolve) => {
    let previousValue = valueGetter();
    let unchangedCount = 0;

    const intervalId = setInterval(() => {
      const currentValue = valueGetter();

      if (currentValue === previousValue) {
        unchangedCount++;
      } else {
        unchangedCount = 0;
      }

      if (unchangedCount >= 2) {
        clearInterval(intervalId);
        resolve(currentValue);
      }

      previousValue = currentValue;
    }, interval);
  });
}

let scrollHeightGetter = () => document.body.scrollHeight;

await waitForValueNotToStopChanging(scrollHeightGetter, 500)

相关问题