你好,我试图模拟一个"自然"使用Selenium和Python中的PhantomJs滚动鼠标。
我想滚动模拟鼠标滚轮以返回页面。 有可能用JavaScript做到这一点吗? Actualy我使用此命令来滚动页面底部。
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
所以我认为可以运行这样的脚本来模拟鼠标的流畅性。
答案 0 :(得分:0)
事实上你到目前为止还没有发布你所尝试的内容 - 顺便说一下。
以下编码风格对我有用 - 可能有更好的做法。
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()