【发布时间】:2021-08-19 08:35:35
【问题描述】:
我在尝试使用 Selenium ChromeDriver 向下滚动 google 地图结果页面的侧边栏时遇到了问题。我正在尝试向下滚动到第 6 个结果,但在您向下滚动之前结果不会完全加载。使用find_element_by_xpath 方法,我成功地能够访问结果1-5 并单独单击它们,但是当尝试使用actions.move_to_element(link).perform() 方法滚动到第6 个元素时,它不起作用并引发错误消息。
我得到的错误是: selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:
但是,我知道这个元素存在,因为当我手动滚动并加载更多结果时,Xpath 可以正常工作。我究竟做错了什么?我花了很多时间试图解决这个问题,但我无法用可用的内容来解决。感谢您提供的任何帮助或见解,谢谢!
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as soup
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.google.com/maps")
time.sleep(7)
page = soup(driver.page_source, 'html.parser')
#find the searchbar, enter search, and hit return
search = driver.find_element_by_id('searchboxinput')
search.send_keys("dentists in Austin Texas")
search.send_keys(Keys.RETURN)
driver.maximize_window()
time.sleep(7)
#I want to get the 6th result down but it requires a sidebar scroll to load
link = driver.find_element_by_xpath("//*[@id='pane']/div/div[1]/div/div/div[4]/div[1]/div[13]/div/a")
actions.move_to_element(link).perform()
link.click()
time.sleep(5)
driver.back()```
【问题讨论】:
-
使用
document.getElementById('id').scrollDown += big_value有效吗? -
感谢回复,我刚试了,还是不行
标签: python html selenium selenium-webdriver scroll