[英]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.