gpt4 book ai didi

javascript - Python中使用Selenium模拟鼠标ScrollUp

转载 作者:行者123 更新时间:2023-11-28 06:50:18 29 4
gpt4 key购买 nike

您好,我正在尝试使用 Selenium 和 Python 中的 PhantomJs 来模拟“自然”的鼠标滚动。

我想模拟鼠标滚轮向上滚动以返回页面。可以用 JavaScript 做到这一点吗?实际上,我使用此命令是为了在页面底部滚动。

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

所以我认为可以运行一些像这样的脚本来模拟鼠标的流畅度。

最佳答案

事实上,到目前为止您还没有发布您尝试过的内容 - 顺便说一句。

以下编码风格适合我 - 可能有更好的实践。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



driver = webdriver.Firefox()

driver.get("https://www.google.com/search?q=a1+plumbing+boise")


link = driver.find_element_by_link_text('View all Google reviews')
link.click()


try:
print "about to look for element"
def find(driver):
e = driver.find_element(By.CLASS_NAME ,'_wu')
if (e.get_attribute("disabled")=='true'):
return False
return e
element = WebDriverWait(driver, 10).until(find)
print "still looking?"
finally: print 'yowp'

nam = driver.find_element(By.CLASS_NAME ,'_wu')

#get length of review
revcnt = driver.find_element(By.XPATH ,"//span[@class='_Mnc _yz']")
revcnt = int(revcnt.text.replace(" reviews","").strip())
print revcnt
# wait for reviews to appear
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.review-snippet")))#review-snippet
cntr = 0
reviews = []
while True:#cntr<=revcnt:
global cntr
reviews = driver.find_elements_by_css_selector("div._ju")
driver.execute_script("arguments[0].scrollIntoView();", reviews[-1])
print len(reviews)
cntr+=1
if len(reviews) == revcnt:
break

mores = driver.find_elements_by_xpath("//a[@class='fl review-more-link']")
for more in mores:
more.click()

js_code = "return document.getElementsByClassName('_ju');"
your_elements = driver.execute_script(js_code)

#print len(your_elements)
for i in your_elements:
print i.text.encode('utf-8')

driver.quit()

关于javascript - Python中使用Selenium模拟鼠标ScrollUp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33012792/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com