我在python中编写了一个与selenium结合使用的脚本,使网页的屏幕向下滚动。内容位于左侧窗口内。如果我向下滚动,可以看到更多项目。我尝试过以下方法,但它似乎不起作用。对此的任何帮助将受到高度赞赏。
请查看:website link。
到目前为止我尝试过:
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("find_the_link_above")
elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#pannello-espositori .luogo")))
for item in range(3):
elem.send_keys(Keys.END)
time.sleep(3)
driver.quit()
当我执行上述脚本时,它会抛出异常can't focus element
。
答案 0 :(得分:3)
尝试使用以下方法:
def scroll_down():
"""A method for scrolling down the page."""
# Get scroll height.
last_height = driver.execute_script("return document.querySelector('#pannello-espositori').scrollHeight;")
while True:
# Scroll down to the bottom.
driver.execute_script("window.iScrollElenco.scrollBy(0, -arguments[0]);", last_height)
# Wait to load the page.
time.sleep(2)
# Calculate new scroll height and compare with last scroll height.
new_height = driver.execute_script("return document.querySelector('#pannello-espositori').scrollHeight;")
if new_height == last_height:
break
last_height = new_height
如果要在左侧面板中向下滚动内容(使用左侧面板的高度),请使用此方法。
希望它对你有所帮助!让我知道结果。
答案 1 :(得分:2)
试试这个。您可以通过向上滚动到左侧面板中的元素来查看滚动效果。
此解决方案将向上滚动至前100个元素。
from selenium import webdriver
import time
def scroll_element_into_view(element):
driver.execute_script(
"arguments[0].scrollIntoView(true);",
element)
time.sleep(0.2) #increase/decrease time as you want delay in your view
driver = webdriver.Chrome()
driver.maximize_window()
driver.set_page_load_timeout(5)
try:
driver.get("http://catalogo.marmomac.it/it/cat")
time.sleep(3)
total_elems= driver.find_elements_by_css_selector(".scroller .elemento")
print len(total_elems)
for i in range(len(total_elems)):
scroll_element_into_view(total_elems[i])
except Exception as e:
print e
finally:
driver.quit()
如前所述,滚动后会加载更多元素。下面的脚本也可以处理。在这里,我们可以使用已经显示在面板顶部的总计数。
对于前计数是:1669
这个过程将持续到1669元素。 (将先前的计数存储在一个变量中,并在每个循环后更新它)
try:
driver.get("http://catalogo.marmomac.it/it/cat")
time.sleep(3)
total_elems=0
total_count = int(driver.find_element_by_css_selector(".totali").text)
while total_elems<total_count:
elems= driver.find_elements_by_css_selector(".scroller .elemento")
found_elms= len(elems)
for i in range(total_elems,found_elms):
scroll_element_into_view(elems[i])
total_elems=found_elms
except Exception as e:
print e
finally:
driver.quit()
答案 2 :(得分:1)
execute_script("arguments[0].scrollIntoView();",element)
选项2
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("my-id")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
答案 3 :(得分:1)
选择器"#pannello-espositori .luogo"
提供面板中第一个元素的部分文本。如果向下滚动,则此元素不再可见,并且可能无法再获取任何命令。您可以找到整个列表,并使用ActionChains
每次滚动到最后一个元素。它将滚动到视图中并重新加载列表
actions = ActionChains(driver)
for item in range(3):
elements = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#pannello-espositori .elemento")))
actions.move_to_element(elements[-1]).perform()