Javascript 在Python中使用Selenium模拟鼠标滚动

Javascript 在Python中使用Selenium模拟鼠标滚动,javascript,jquery,python,selenium,mousewheel,Javascript,Jquery,Python,Selenium,Mousewheel,您好,我正在尝试使用Selenium和Python中的PhantomJs模拟“自然”鼠标滚动 我想模拟鼠标滚轮向上滚动,以便返回页面。 是否可以用JavaScript实现这一点? 实际上,我使用这个命令是为了在页面底部滚动 self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 因此,我认为可以运行类似这样的脚本来模拟鼠标的流畅性。事实上,你没有发布到目前为止你尝试过的内容 下面的编码风格对我很

您好,我正在尝试使用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()
从selenium导入webdriver
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
driver=webdriver.Firefox()
驱动程序。获取(“https://www.google.com/search?q=a1+管道+博伊西)
link=驱动程序。通过链接文本查找元素('查看所有谷歌评论')
link.click()
尝试:
打印“即将查找元素”
def查找(驱动程序):
e=驱动程序.查找元素(按.CLASS\u名称,“\u-wu”)
如果(e.get_属性(“disabled”)=“true”):
返回错误
返回e
element=WebDriverWait(驱动程序,10)。直到(查找)
打印“还在看吗?”
最后:打印“yowp”
nam=驱动程序.find_元素(By.CLASS_名称,“u-wu”)
#获取评论的长度
revcnt=driver.find_元素(By.XPATH,“//span[@class=”\u Mnc\u yz']))
revcnt=int(revcnt.text.replace(“reviews”,”).strip())
打印版本
#等待评论出现
wait=WebDriverWait(驱动程序,10)
等待.until(位于((By.CSS_选择器,“span.review snippet”))#查看代码段
cntr=0
评论=[]

虽然是真的:#cntr事实上,到目前为止,你还没有发布你尝试过的东西——顺便说一句

下面的编码风格对我很有用-可能有更好的实践

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()
从selenium导入webdriver
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
driver=webdriver.Firefox()
驱动程序。获取(“https://www.google.com/search?q=a1+管道+博伊西)
link=驱动程序。通过链接文本查找元素('查看所有谷歌评论')
link.click()
尝试:
打印“即将查找元素”
def查找(驱动程序):
e=驱动程序.查找元素(按.CLASS\u名称,“\u-wu”)
如果(e.get_属性(“disabled”)=“true”):
返回错误
返回e
element=WebDriverWait(驱动程序,10)。直到(查找)
打印“还在看吗?”
最后:打印“yowp”
nam=驱动程序.find_元素(By.CLASS_名称,“u-wu”)
#获取评论的长度
revcnt=driver.find_元素(By.XPATH,“//span[@class=”\u Mnc\u yz']))
revcnt=int(revcnt.text.replace(“reviews”,”).strip())
打印版本
#等待评论出现
wait=WebDriverWait(驱动程序,10)
等待.until(位于((By.CSS_选择器,“span.review snippet”))#查看代码段
cntr=0
评论=[]

虽然是真的:#cntrHave你能解决问题到目前为止:)?@SIslam不,我发现唯一类似的是:self.driver.execute_script($('html,body').animate({scrollTop:0},2000);),但只有当网站实现了动画功能时它才起作用。你能发布网站链接给我一个清晰的想法吗?@SIslam啊!我试图让这个网站接受测试。到目前为止,你能解决这个问题吗:)?@SIslam不,我发现唯一类似的是:self.driver.execute_script($('html,body')).animate({scrollTop:0},2000);),但只有当网站实现了animate功能时,它才能工作。你能发布网站链接给我一个清晰的想法吗?@SIslam啊!我试图让网站接受测试。