Hướng dẫn scroll up in selenium python - cuộn lên trong trăn selen

19

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 đang gặp vấn đề với việc cuộn lên đầu trang web khi sử dụng Python và Selenium.

Khi trang tải cho một số lý do bạn được đưa xuống cuối trang (điều này là do được sửa). Tuy nhiên, khi tôi đang cố gắng cuộn lên trên cùng, nó không hoạt động.

Tôi đã thử như sau:

self.driver.execute_script("scroll(0, -250);")

self.driver.execute_script("scroll(0, 0);")

Tôi cũng đã thử định vị phần tử sau đó cuộn vào nó:

self.driver.execute_script("arguments[0].scrollIntoView()", element)

Mã ScrollintOview () ở trên hoạt động khi cuộn xuống phần tử. Tuy nhiên, nó không hoạt động cuộn lên.

Tôi đã thử trình điều khiển Chrome và Phantomjs đang chạy này.

Bất kỳ đề xuất?

Hướng dẫn scroll up in selenium python - cuộn lên trong trăn selen

Hỏi ngày 8 tháng 8 năm 2017 lúc 19:50Aug 8, 2017 at 19:50

4

Bạn chỉ có thể sử dụng các phím

self.driver.execute_script("scroll(0, 0);")
1 +
self.driver.execute_script("scroll(0, 0);")
2. Nó sẽ cuộn lên đầu trang.

from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)

Hướng dẫn scroll up in selenium python - cuộn lên trong trăn selen

Cottectail

4.22118 Huy hiệu vàng18 Huy hiệu bạc35 Huy hiệu đồng18 gold badges18 silver badges35 bronze badges

Đã trả lời ngày 9 tháng 8 năm 2017 lúc 11:20Aug 9, 2017 at 11:20

Hướng dẫn scroll up in selenium python - cuộn lên trong trăn selen

SunilthoratsunilthoratSunilThorat

1.5722 huy hiệu vàng11 Huy hiệu bạc15 Huy hiệu đồng2 gold badges11 silver badges15 bronze badges

3

Bạn có thể xem xét để xác định vị trí phần tử trong

self.driver.execute_script("scroll(0, 0);")
3 trước, sau đó chúng ta có thể
self.driver.execute_script("scroll(0, 0);")
4 phần tử vào
self.driver.execute_script("scroll(0, 0);")
5 như sau:
self.driver.execute_script("scroll(0, 0);")
5
as follows:

element = driver.find_element_by_xpath("element_xpath")
self.driver.execute_script("return arguments[0].scrollIntoView(true);", element)

Đã trả lời ngày 9 tháng 8 năm 2017 lúc 4:43Aug 9, 2017 at 4:43

Hướng dẫn scroll up in selenium python - cuộn lên trong trăn selen

0

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("__")

#to scroll try use the following command
driver.execute_script("scrollBy(0,250);")

Nó sẽ hoạt động !!

Hướng dẫn scroll up in selenium python - cuộn lên trong trăn selen

Hamza Anis

2.3571 Huy hiệu vàng24 Huy hiệu bạc35 Huy hiệu Đồng1 gold badge24 silver badges35 bronze badges

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

VyrvyrVyr

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

1

Vui lòng thử điều này:

driver.execute_script("document.querySelector('div[role=dialog] ul').parentNode.scrollTop=1e100")

Đã trả lời ngày 12 tháng 1 năm 2019 lúc 8:17Jan 12, 2019 at 8:17

Hamed Baziyadhamed BaziyadHamed Baziyad

1.8545 huy hiệu vàng26 Huy hiệu bạc36 Huy hiệu đồng5 gold badges26 silver badges36 bronze badges

từ Selenium Nhập WebDriver

t=10
while t:

#if you want to scroll to the end of the page,use this

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


#if you want to scroll down upto some level use this, here i used "1000" you may vary 
#it according to your use

driver.execute_script("scrollBy(0,+1000);")
    sleep(3)


#if you want to scroll some level up, use this,again i used here "-500" you may vary 
#according to your use 

driver.execute_script("scrollBy(0,-500);")
sleep(3)
t=t-1       # it`s a part of the loop

Điều này chắc chắn sẽ giúp bạn :)

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

Hướng dẫn scroll up in selenium python - cuộn lên trong trăn selen

Có 4 cách để cuộn lên hoặc xuống

1) Cuộn bằng pixel

driver.execute_script("window.scrollBy(0,0)","")

2) Cuộn xuống cho đến khi phần tử không được tìm thấy

element=driver.find_element(By.XPATH,"xpath of element")
driver.execute_script("arguments[0].scrollIntoView();",element)

3) Cuộn cho đến cuối trang

self.driver.execute_script("scroll(0, 0);")
0
  1. Sử dụng chuỗi hành động

    ElementPos = Driver.find_element (by.xpath, "XPath of Element") action = actionChains (trình điều khiển) action.move_to_element (ElementPos) .Perform ()
    actions=ActionChains(driver) actions.move_to_element(elementpos).perform()

Đã trả lời ngày 27 tháng 10 năm 2020 lúc 7:50Oct 27, 2020 at 7:50

Hướng dẫn scroll up in selenium python - cuộn lên trong trăn selen

Bạn có thể sử dụng document.body.scrolltop: Một biến JavaScript chứa giá trị theo những gì trang đã được cuộn xuống trước đó. Như thế này:

self.driver.execute_script("scroll(0, 0);")
6

Đã trả lời ngày 7 tháng 9 năm 2021 lúc 20:53Sep 7, 2021 at 20:53

0

Làm cách nào để cuộn lên trong Selenium Python?

Để cuộn lên trên cùng của trang web bằng selenium, bạn có thể sử dụng hàm Execute_script () để thực thi Window hàm JavaScript.ScrollTo () và chuyển '0' cho tham số thứ hai.use the execute_script() function to execute the JavaScript function window. scrollTo() and pass '0' for the second parameter.

Làm thế nào để bạn tự động hóa cuộn lên trong selenium?

JavaScriptexecutor cung cấp một giao diện cho phép QAS chạy các phương thức JavaScript từ các tập lệnh selen.Do đó, để cuộn lên hoặc xuống với selenium, một javascriptexecutor là phải.Phương thức cuộn () bao gồm hai tham số, x và y, đại diện cho các giá trị pixel ngang và dọc tương ứng.The scrollBy() method involves two parameters, x, and y, that represent the horizontal and vertical pixel values, respectively.

Làm cách nào để cuộn theo chiều dọc trong selenium?

Selenium có thể thực thi các lệnh JavaScript với sự trợ giúp của phương thức thực thi.Để cuộn xuống theo chiều dọc trong một trang, chúng ta phải sử dụng cửa sổ lệnh JavaScript.scrollby.Sau đó chuyển các giá trị pixel để đi dọc theo trục X và Y cho cuộn ngang và dọc tương ứng.use the JavaScript command window. scrollBy. Then pass the pixel values to be traversed along the x and y axis for horizontal and vertical scroll respectively.

Làm cách nào để cuộn lên và cuộn xuống trong selenium?

Chúng ta có thể cuộn trang lên hoặc xuống trong selenium webdriver bằng Java.Điều này đạt được với sự trợ giúp của lớp hành động.Trước hết, chúng ta phải tạo một đối tượng của lớp hành động này và sau đó áp dụng phương thức SendKeys cho nó. Bây giờ, để cuộn xuống một trang, chúng ta phải chuyển các khóa tham số.create an object of this Actions class and then apply the sendKeys method to it. Now, to scroll down a page, we have to pass the parameter Keys.