模拟鼠标滚轮以加载所有元素。



我搜索了Google并尝试了很多,但是失败了。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Configuration information
email = "[email protected]"
password = "Huangbo1019@"

driver = webdriver.Chrome()
index_url = "https://quip.com/"
driver.get(url=index_url)
driver.find_element_by_xpath('//*[@id="header-nav-collapse"]/ul/li[9]/a').click()  # click login
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[1]/form/div/input').send_keys(email)  # input email
driver.find_element_by_xpath('//*[@id="email-submit"]').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div/div/form/div/input[2]').send_keys(password)  # input password
driver.find_element_by_xpath('/html/body/div/div/form/button').click()
time.sleep(2)


下拉滚动

js="var q=document.documentElement.scrollTop=100000" driver.execute_script(js)
time.sleep(3)


driver.maximize_window()
driver.find_element_by_xpath("xpath").send_keys(Keys.DOWN)


js="var q=document.documentElement.scrollTop=10000"
driver.execute_script(js)


上面的方法不起作用。

最佳答案

要向下滚动网页https://testselenium.quip.com/直到最后一个条目,您必须为visibility_of_element_located()引入WebDriverWait,并且可以使用以下Locator Strategies


代码块:

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

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
#chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
#chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://quip.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn-sm[href='/account/login']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("[email protected]")
driver.find_element_by_css_selector("button#email-submit").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys("Huangbo1019@")
driver.find_element_by_css_selector("button.submit").click()
driver.execute_script("arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.nav-inbox-import-title"))))

浏览器快照:


javascript -  Selenium 鼠标滚轮下拉以加载所有页面-LMLPHP


  在这里您可以找到有关What is the difference between the different scroll options?的详细讨论。

08-05 23:57