简体   繁体   English

使用 Selenium 更改 Google 地图评论排序

[英]Change Google Maps review sort with Selenium


I am facing interesting issue with my **Web Scraping** use case. 我的 **Web Scraping** 用例遇到了有趣的问题。 I need to get newest **Google Maps reviews**. 我需要获取最新的 **Google 地图评论**。
I want to sort reviews by latest date. 我想按最新日期对评论进行排序。 And all tutorials I am watching is in English, but in my native language the UI is different than in those tutorials. 我正在观看的所有教程都是英文的,但在我的母语中,用户界面与那些教程不同。
I am able to click on the button using **Selenium** and button's **XPATH**, but I don't know how to change the sorting options from the visible drop menu. 我可以使用 **Selenium** 和按钮的 **XPATH** 单击按钮,但我不知道如何从可见的下拉菜单中更改排序选项。
 # Click the sort button driver.find_element_by_xpath('//*[@id="pane"]/div/div[1]/div/div/div[2]/div[8]/button').click()

铬检查

select_by_visible_text() and select_by_value() doesn't work for me as I cannot select the button and doesn't work on div. select_by_visible_text()select_by_value()对我不起作用,因为我不能 select 按钮并且在 div 上不起作用。

URL I am using: Link URL 我正在使用: 链接
To see my UI change to LITHUANIAN language.看到我的用户界面更改为立陶宛语。

First of all you have to learn how to create correct XPath locators.首先,您必须学习如何创建正确的 XPath 定位器。
Long XPath expressions are too fragile.长的 XPath 表达式太脆弱了。
The "Sort Reviews" button locator instead of “排序评论”按钮定位器而不是
//*[@id="pane"]/div/div[1]/div/div/div[2]/div[8]/button can be //*[@id="pane"]/div/div[1]/div/div/div[2]/div[8]/button可以
//button[@aria-label='Sort reviews'] or //button[@aria-label='Sort reviews']
//button[@data-value='Sort']
After clicking this button, to sort reviews by latest date you can click this element: //li[@data-index='1']单击此按钮后,要按最新日期对评论进行排序,您可以单击此元素: //li[@data-index='1']
So basically this will work:所以基本上这会起作用:

driver.find_element_by_xpath("//li[@data-index='1']").click()

But since you need to wait for dialog to open after clicking the Sort button you need to utilize Expected Condition wait, as following:但是由于您需要在单击排序按钮后等待对话框打开,您需要利用预期条件等待,如下所示:

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@data-index='1']"))).click()

You will need the following imports for this:为此,您将需要以下导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

This code should work.这段代码应该可以工作。

I added a webdriverwait after clicking on the 'Sort' button to wait until the all the options in the dropdown are visible, then clicked on 'Highest rating'.单击“排序”按钮后,我添加了一个 webdriverwait 等待直到下拉列表中的所有选项都可见,然后单击“最高评分”。 //li[@role='menuitemradio'])[3] refers to the 3rd element in the dropdown which is the 'Higest rating'. //li[@role='menuitemradio'])[3]指的是下拉列表中的第三个元素,即“最高评分”。 I tried using the text to be specific and not count on element index, but somehow it is not working.我尝试使用特定的文本而不依赖元素索引,但不知何故它不起作用。 But the below code does sort the reviews.但是下面的代码确实对评论进行了排序。

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
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.google.com/maps/place/Senukai/@54.6832836,25.183965,12z/data=!4m11!1m2!2m1!1svilnius+senukai!3m7!1s0x46dd94055529fabf:0xb1132b0ad981d43b!8m2!3d54.7098368!4d25.2999662!9m1!1b1!15sCg92aWxuaXVzIHNlbnVrYWkiA4gBAVoRIg92aWxuaXVzIHNlbnVrYWmSARhidWlsZGluZ19tYXRlcmlhbHNfc3RvcmU")
print(driver.title)
time.sleep(5)
driver.find_element(By.XPATH, "//button[@data-value='Sort']").click()
WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//li[@role='menuitemradio']")))
driver.find_element(By.XPATH, "(//li[@role='menuitemradio'])[3]").click()
time.sleep(2)
driver.quit()

PS I used time.sleep occasionally to make a quick code, but it would be a good practice to use WebdriverWait in lieu of time.sleep PS 我偶尔使用time.sleep来快速编写代码,但使用time.sleep代替WebdriverWait是一个好习惯

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

相关问题 Python selenium 用于撰写评论谷歌地图(selenium.common.exceptions.InvalidArgumentException:消息:无效参数:无效定位器) - Python selenium for write review google maps (selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator ) Python Selenium 不会改变滚动高度谷歌地图评论 - Python Selenium not change Scroll Height google maps reviews 使用 python selenium 抓取谷歌地图 - webscraping google maps with python selenium 通过 Selenium 在 Python 中抓取谷歌地图 - Scraping Google Maps by Selenium in Python 刮谷歌地图审查一家公司的文本数据 - Scrape google maps review text data for one company 谷歌地图使用硒的地方ID - Google maps place id using selenium 使用 Selenium 和 Google Maps API 的屏幕截图地图 - Screenshot Map using Selenium and Google Maps API 在Google地图中更改标记符号的颜色-Python - Change the color of Marker Symbol in Google maps - Python 在谷歌地图上使用chrome webdriver在python3 + selenium中滚动浏览元素 - scrolling through an element in python3+selenium with chrome webdriver on google maps Web 使用 Selenium 抓取谷歌地图使用了太多数据 - Web scraping Google Maps with Selenium uses too much data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM