[英]Change Google Maps review sort with Selenium
# 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.