简体   繁体   English

如何使用 selenium ChromeDriver 在 Google 地图上滚动侧边栏以加载更多结果?

[英]How do I use selenium ChromeDriver to scroll the sidebar on Google maps to load more results?

I've run into a problem trying to use Selenium ChromeDriver to scroll down the sidebar of a google maps results page.我在尝试使用 Selenium ChromeDriver 向下滚动谷歌地图结果页面的侧边栏时遇到了问题。 I am trying to get to the 6th result down but the result does not fully load until you scroll down.我正在尝试向下滚动到第 6 个结果,但在您向下滚动之前结果不会完全加载。 Using the find_element_by_xpath method, I am successfully able to access results 1-5 and click into them individually, but when trying to use the actions.move_to_element(link).perform() method to scroll to the 6th element, it does not work and throws an error message.使用find_element_by_xpath方法,我可以成功访问结果 1-5 并单独单击它们,但是当尝试使用actions.move_to_element(link).perform()方法滚动到第 6 个元素时,它不起作用并且引发错误消息。

The error that I get is: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:我得到的错误是:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:

However, I know this element exists because when I manually scroll and more results are loaded, the Xpath works correctly.但是,我知道这个元素存在,因为当我手动滚动并加载更多结果时,Xpath 工作正常。 What am I doing wrong?我究竟做错了什么? I've spent many hours trying to solve this and I haven't been able to solve with the available content out there.我花了很多时间试图解决这个问题,但我无法用可用的内容来解决。 I appreciate any help or insights you can offer, thank you!感谢您提供的任何帮助或见解,谢谢!

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()```

The search results in the google map are located with //div[contains(@aria-label,'dentists in Austin Texas')]//div[contains(@jsaction,'mouseover')] XPath. google map 中的搜索结果位于//div[contains(@aria-label,'dentists in Austin Texas')]//div[contains(@jsaction,'mouseover')] XPath。
So, to select 6-th element there you can do the following因此,对于 select 的第 6 个元素,您可以执行以下操作

from selenium.webdriver.common.action_chains import ActionChains

results = driver.find_elements_by_xpath('//div[contains(@aria-label,"dentists in Austin Texas")]//div[contains(@jsaction,"mouseover")]')

ActionChains(driver).move_to_element(results[6]).click(button).perform()

I found a solution that works, it is to target the element in XPATH from the javascript interface of selenium.我找到了一个可行的解决方案,它是从 selenium 的 javascript 接口定位 XPATH 中的元素。 You must then execute two commands on an instruction (targeting and scroll)然后,您必须在一条指令上执行两个命令(定位和滚动)

driver.executeScript("var el = document.evaluate('/html/body/jsl/div[3]/div[10]/div[8]/div/div[1]/div/div/div[4]/div[1]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; el.scroll(0, 5000);");

this is the only solution that worked for me这是唯一对我有用的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Google Chrome 而不是 Chromedriver Python Selenium - How do I use Google Chrome instead of Chromedriver Python Selenium 如何使用 selenium 滚动谷歌地图搜索显示的结果 - How to scroll the results shown by google maps search using selenium 如何使用 Selenium 到 Python 单击 Google 地图侧边栏菜单 - How can I click on Google Maps sidebar menu using Selenium through Python 如何使用 Selenium (Python) 进行 Google 搜索,然后在新选项卡中打开第一页的结果? - How can I use Selenium (Python) to do a Google Search and then open the results of the first page in new tabs? 如何使用带有 selenium 的 open chromedriver - how to use open chromedriver with selenium 如何从 Google 搜索结果页面中抓取所有结果 (Python/Selenium ChromeDriver) - How to scrape all results from Google search results pages (Python/Selenium ChromeDriver) 如何使用 selenium python 向下滚动谷歌地图 - How to scroll down google maps using selenium python 将chromedriver与Selenium结合使用时,如何更正超时错误? - While using the chromedriver with Selenium, how do I correct a timeout error? 如何使用chromedriver使用硒在网页中找到这些特定元素? - How do I find these particular elements in a webpage, with selenium using chromedriver? 如何将 selenium 和 chromedriver 添加到 AWS Lambda function? - How do I add selenium & chromedriver to an AWS Lambda function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM