簡體   English   中英

在 Python Selenium 中的特定 DIV 元素上滾動

[英]Scroll on a specific DIV Element in Python Selenium

我正在嘗試做一個簡單的 Python Selenium 自動化,其中腳本將單擊一個鏈接,該鏈接將在頁面頂部打開一個對話框(Instagram 個人資料)。

上述對話框將顯示關注者列表,但不幸的是,包含該列表的 UL 將僅顯示前 12 個關注者(或 LI)。 它由 AJAX 提供支持,以“加載更多”關注者。

無論如何,為了模擬加載更多關注者,我嘗試了以下代碼:

driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/ul').send_keys(Keys.END)

或者

driver.find_element_by_tag_name('body').send_keys(Keys.END)

不幸的是,它不起作用。 我想知道是否有正確的方法來做到這一點(向下滾動專注於活動 div 或任何頁面元素)?

提供下圖以顯示所述頁面的 html 結構。

在此處輸入圖像描述

感謝您對此的幫助,非常感謝!

你可以試試這樣的東西嗎? 這將滾動到您提到的 div 元素。

Python代碼

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

Java樣品:

WebElement element = driver.findElement(By.id("id_of_div_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

用這個

liElement = driver.find_element_by_xpath("//div[@role='dialog']//ul//li[text()='required_li_element_visible_text']")
driver.execute_script("arguments[0].scrollIntoView(true);", liElement);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM