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];"]
Và
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ỏ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]
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
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
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 !!
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
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];"]
0Sử 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
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