Hướng dẫn scroll selenium python - cuộn trăn selen

223

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi hiện đang sử dụng Selenium WebDriver để phân tích thông qua trang bạn bè người dùng Facebook và trích xuất tất cả ID từ tập lệnh Ajax. Nhưng tôi cần phải cuộn xuống để có được tất cả bạn bè. Làm thế nào tôi có thể cuộn xuống trong selen. Tôi đang sử dụng Python.

Hỏi ngày 8 tháng 1 năm 2014 lúc 3:44Jan 8, 2014 at 3:44

Hướng dẫn scroll selenium python - cuộn trăn selen

3

Bạn có thể dùng

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

trong đó y là chiều cao (trên màn hình FullHD là 1080). (Cảm ơn @lukeis)

Bạn cũng có thể dùng

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

để cuộn xuống cuối trang.the bottom of the page.

Nếu bạn muốn cuộn đến một trang có tải vô hạn, như mạng xã hội, Facebook, v.v. (cảm ơn @cuong tran)to scroll to a page with infinite loading, like social network ones, facebook etc. (thanks to @Cuong Tran)

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

một phương pháp khác (nhờ Juanse) là, chọn một đối tượng và

label.sendKeys(Keys.PAGE_DOWN);

Đã trả lời ngày 3 tháng 1 năm 2015 lúc 22:13Jan 3, 2015 at 22:13

Hướng dẫn scroll selenium python - cuộn trăn selen

OwadvlowadvlOWADVL

10.1k6 Huy hiệu vàng53 Huy hiệu bạc67 Huy hiệu Đồng6 gold badges53 silver badges67 bronze badges

4

Nếu bạn muốn cuộn xuống dưới cùng của trang Infinite (như LinkedIn.com), bạn có thể sử dụng mã này:scroll down to bottom of infinite page (like linkedin.com), you can use this code:

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

Tham khảo: https://stackoverflow.com/a/28928684/1316860

Đã trả lời ngày 8 tháng 4 năm 2017 lúc 19:32Apr 8, 2017 at 19:32

Cuong Trancuong TranCuong Tran

1.79919 huy hiệu bạc21 Huy hiệu đồng19 silver badges21 bronze badges

3

Bạn có thể sử dụng

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
4 để mô phỏng phím
SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
5 (hoặc
SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
6) (thường cuộn trang):

from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)

Đã trả lời ngày 15 tháng 7 năm 2018 lúc 5:34Jul 15, 2018 at 5:34

Hướng dẫn scroll selenium python - cuộn trăn selen

Liu Yueliu YueLIU YUE

1.31710 Huy hiệu bạc16 Huy hiệu đồng10 silver badges16 bronze badges

1

Phương pháp tương tự như được hiển thị ở đây:

Trong Python bạn chỉ có thể sử dụng

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

(Y là vị trí thẳng đứng bạn muốn cuộn đến)

Hướng dẫn scroll selenium python - cuộn trăn selen

Parik

2.17512 Huy hiệu vàng38 Huy hiệu bạc65 Huy hiệu Đồng12 gold badges38 silver badges65 bronze badges

Đã trả lời ngày 8 tháng 1 năm 2014 lúc 4:04Jan 8, 2014 at 4:04

Lukeislukeislukeis

1.9632 huy hiệu vàng19 Huy hiệu bạc19 Huy hiệu đồng2 gold badges19 silver badges19 bronze badges

0

element=find_element_by_xpath("xpath of the li you are trying to access")

element.location_once_scrolled_into_view

Điều này có ích khi tôi đang cố gắng truy cập vào một 'li' mà không thể nhìn thấy.

Hướng dẫn scroll selenium python - cuộn trăn selen

Pang

9.222146 Huy hiệu vàng85 Huy hiệu bạc118 Huy hiệu đồng146 gold badges85 silver badges118 bronze badges

Đã trả lời ngày 7 tháng 6 năm 2016 lúc 22:54Jun 7, 2016 at 22:54

PremonitionPremonitionpremonition

2593 Huy hiệu bạc7 Huy hiệu Đồng3 silver badges7 bronze badges

2

Với mục đích của tôi, tôi muốn cuộn xuống nhiều hơn, ghi nhớ vị trí cửa sổ. Giải pháp của tôi tương tự và được sử dụng

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
7

driver.execute_script("window.scrollTo(0, window.scrollY + 200)")

sẽ đi đến vị trí cuộn y hiện tại + 200

Đã trả lời ngày 2 tháng 8 năm 2018 lúc 16:59Aug 2, 2018 at 16:59

Hướng dẫn scroll selenium python - cuộn trăn selen

Nick Bradynick BradyNick Brady

5.7281 Huy hiệu vàng44 Huy hiệu bạc66 Huy hiệu đồng1 gold badge44 silver badges66 bronze badges

Đây là cách bạn cuộn xuống trang web:

driver.execute_script("window.scrollTo(0, 1000);")

Oguz Ismail

41.1K12 Huy hiệu vàng43 Huy hiệu bạc64 Huy hiệu đồng12 gold badges43 silver badges64 bronze badges

Đã trả lời ngày 28 tháng 11 năm 2018 lúc 7:14Nov 28, 2018 at 7:14

Hướng dẫn scroll selenium python - cuộn trăn selen

Không có câu trả lời nào trong số này làm việc cho tôi, ít nhất là không phải để cuộn xuống trang kết quả tìm kiếm trên Facebook, nhưng tôi đã tìm thấy sau rất nhiều thử nghiệm giải pháp này:

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

Đã trả lời ngày 9 tháng 11 năm 2017 lúc 12:37Nov 9, 2017 at 12:37

Bass Debass DeeBass Dee

811 Huy hiệu bạc3 Huy hiệu đồng1 silver badge3 bronze badges

1

Cách dễ nhất tôi tìm thấy để giải quyết vấn đề đó là chọn một nhãn và sau đó gửi:

label.sendKeys(Keys.PAGE_DOWN);

Hy vọng nó hoạt động!

Đã trả lời ngày 16 tháng 4 năm 2018 lúc 18:21Apr 16, 2018 at 18:21

Hướng dẫn scroll selenium python - cuộn trăn selen

JuansejuanseJuanse

811 Huy hiệu bạc1 Huy hiệu đồng1 silver badge1 bronze badge

0

Các trang tải cuộn. Ví dụ: Trung bình, Quora, v.v.

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

Đã trả lời ngày 22 tháng 4 năm 2019 lúc 12:54Apr 22, 2019 at 12:54

2

Khi làm việc với YouTube, các yếu tố nổi cho giá trị "0" vì chiều cao cuộn vì vậy thay vì sử dụng "return document.body.scrollHeight" Hãy thử sử dụng một "return tài liệu return.documentEuity.scrollHeight" điều chỉnh thời gian tạm dừng cuộn theo Internet của bạn Tốc độ khác, nó sẽ chỉ chạy một lần và sau đó phá vỡ sau đó."return document.body.scrollHeight" try using this one "return document.documentElement.scrollHeight" adjust the scroll pause time as per your internet speed else it will run for only one time and then breaks after that.

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

Đã trả lời ngày 13 tháng 3 năm 2019 lúc 4:35Mar 13, 2019 at 4:35

Hướng dẫn scroll selenium python - cuộn trăn selen

Vinay Vermavinay VermaVinay Verma

5975 Huy hiệu bạc12 Huy hiệu Đồng5 silver badges12 bronze badges

Mã này cuộn xuống phía dưới nhưng không yêu cầu bạn chờ đợi mỗi lần. Nó sẽ liên tục cuộn, sau đó dừng ở phía dưới (hoặc thời gian chờ)

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

Điều này nhanh hơn nhiều so với chờ 0,5-3 giây mỗi lần để trả lời, khi phản hồi đó có thể mất 0,1 giây

Đã trả lời ngày 11 tháng 7 năm 2019 lúc 1:20Jul 11, 2019 at 1:20

OsuynonmaosuynonmaOsuynonma

4291 Huy hiệu vàng6 Huy hiệu bạc12 Huy hiệu đồng1 gold badge6 silver badges12 bronze badges

1

Dưới đây là một đoạn mã mã selen mà bạn có thể sử dụng cho loại mục đích này. Nó được chuyển đến URL cho kết quả tìm kiếm trên YouTube trên 'Tái tạo hướng dẫn Python' và cuộn xuống cho đến khi tìm thấy video với tiêu đề: 'Tái tạo hướng dẫn Python (2020).'

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

Đã trả lời ngày 7 tháng 8 năm 2020 lúc 11:56Aug 7, 2020 at 11:56

coder420coder420coder420

1532 Huy hiệu bạc8 Huy hiệu Đồng2 silver badges8 bronze badges

Tôi đã tìm kiếm một cách cuộn qua một trang web động và tự động dừng sau khi kết thúc trang và tìm thấy chủ đề này.

Bài đăng của @cuong Tran, với một sửa đổi chính, là câu trả lời mà tôi đang tìm kiếm. Tôi nghĩ rằng những người khác có thể thấy sửa đổi hữu ích (nó có ảnh hưởng rõ rệt về cách thức hoạt động của mã), do đó bài đăng này.

Việc sửa đổi là di chuyển câu lệnh ghi lại chiều cao trang cuối cùng bên trong vòng lặp (để mỗi lần kiểm tra so sánh với chiều cao trang trước).inside the loop (so that each check is comparing to the previous page height).

Vì vậy, mã dưới đây:

Liên tục cuộn xuống một trang web động (

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
8), chỉ dừng khi, đối với một lần lặp, chiều cao trang vẫn giữ nguyên.

.

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

Đã trả lời ngày 3 tháng 9 năm 2018 lúc 18:21Sep 3, 2018 at 18:21

Mylesmylesmyles

1651 Huy hiệu vàng3 Huy hiệu bạc8 Huy hiệu đồng1 gold badge3 silver badges8 bronze badges

0

Bạn có thể sử dụng Send_Keys để mô phỏng phím Page_DOWN Bấm (thường cuộn trang):send_keys to simulate a PAGE_DOWN key press (which normally scroll the page):

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

Đã trả lời ngày 8 tháng 9 năm 2020 lúc 14:19Sep 8, 2020 at 14:19

Hướng dẫn scroll selenium python - cuộn trăn selen

3

Nếu bạn muốn cuộn trong một chế độ xem/khung cụ thể (WebEuity), điều bạn chỉ cần làm là thay thế "cơ thể" bằng một yếu tố cụ thể mà bạn dự định sẽ cuộn bên trong. Tôi nhận được yếu tố đó thông qua "getEuityByid" trong ví dụ dưới đây:within a particular view/frame (WebElement), what you only need to do is to replace "body" with a particular element that you intend to scroll within. i get that element via "getElementById" in the example below:

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

Đây là trường hợp trên YouTube, ví dụ ...YouTube, for example...

Đã trả lời ngày 13 tháng 1 năm 2020 lúc 10:01Jan 13, 2020 at 10:01

Hàm

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
9 không hoạt động nữa. Đây là những gì tôi đã sử dụng và nó hoạt động tốt.

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

Đã trả lời ngày 18 tháng 3 năm 2020 lúc 10:09Mar 18, 2020 at 10:09

MokgmokgMoKG

2563 Huy hiệu bạc12 Huy hiệu Đồng3 silver badges12 bronze badges

2

Theo các tài liệu, lớp

label.sendKeys(Keys.PAGE_DOWN);
0 làm công việc:

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
0

Đã trả lời ngày 14 tháng 5 lúc 13:21May 14 at 13:21

Chèn dòng này

label.sendKeys(Keys.PAGE_DOWN);
1

Đã trả lời ngày 15 tháng 1 năm 2021 lúc 6:14Jan 15, 2021 at 6:14

Hướng dẫn scroll selenium python - cuộn trăn selen

1

Vòng lặp bằng phương thức "Gửi phím" để cuộn trang:

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
1

Đã trả lời ngày 16 tháng 3 lúc 5:35Mar 16 at 5:35

Grantrgrantrgrantr

4947 Huy hiệu bạc10 Huy hiệu đồng7 silver badges10 bronze badges

Đây là một phương pháp tôi đã viết để từ từ cuộn xuống một phần tử mục tiêu

Bạn có thể vượt qua vị trí của phần tử của bộ chọn CSS cho nó

Nó cuộn chính xác như chúng ta làm qua bánh xe chuột

Khi phương thức này được gọi, bạn gọi lại với cùng một đối tượng trình điều khiển nhưng với phần tử mục tiêu mới, sau đó nó sẽ cuộn lên/xuống bất cứ nơi nào phần tử đó tồn tại

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
2

Đã trả lời ngày 26 tháng 6 lúc 13:17Jun 26 at 13:17

Umair ayubumair ayubUmair Ayub

Phim thương hiệu vàng 17K1414 gold badges67 silver badges143 bronze badges

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
3

Nó đang làm việc cho trường hợp của tôi.

Paul Roub

36K27 Huy hiệu vàng80 Huy hiệu bạc88 Huy hiệu đồng27 gold badges80 silver badges88 bronze badges

Đã trả lời ngày 11 tháng 6 năm 2020 lúc 13:44Jun 11, 2020 at 13:44

Hướng dẫn scroll selenium python - cuộn trăn selen