Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium web驱动程序:从下拉列表中选择值(无此类元素异常)_Python_Python 3.x_Selenium_Xpath_Webdriverwait - Fatal编程技术网

Python selenium web驱动程序:从下拉列表中选择值(无此类元素异常)

Python selenium web驱动程序:从下拉列表中选择值(无此类元素异常),python,python-3.x,selenium,xpath,webdriverwait,Python,Python 3.x,Selenium,Xpath,Webdriverwait,我正在尝试使用SeleniumWebDriver从google应用程序中提取用户评论。我在网络浏览器上加载了所有评论。现在我想将review页面的“最相关”选项更改为“最新”选项,如图所示。 这是我的密码: from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions

我正在尝试使用SeleniumWebDriver从google应用程序中提取用户评论。我在网络浏览器上加载了所有评论。现在我想将review页面的“最相关”选项更改为“最新”选项,如图所示。

这是我的密码:

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

driver = webdriver.Chrome()
baseurl = 'https://play.google.com/store/apps/details?id=com.mapmyrun.android2&showAllReviews=true'
driver.get(baseurl)

driver.find_element_by_xpath("//div[@class='OA0qNb ncFHed']//div[@class='MocG8c UFSXYb LMgvRb KKjvXb']//span[@class='vRMGwf oJeWuf']").click()
当我运行代码时,它抛出以下错误:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='OA0qNb ncFHed']//div[@class='MocG8c UFSXYb LMgvRb KKjvXb']//span[@class='vRMGwf oJeWuf']"}

如果您想简单地单击div并单击“最新”。您的类是动态的,并且会发生变化。最好得到一个不变的标识

driver.get("https://play.google.com/store/apps/details?id=com.mapmyrun.android2&showAllReviews=true")
driver.find_element_by_xpath("//span[text()='Most relevant']/parent::div").click()
time.sleep(3)
driver.find_elements_by_xpath("//span[text()='Newest']")[1].click()

点击
googlereview options
as
newst
include
WebDriverWait
(),等待
元素可点击
(),然后点击
xpath
选项

driver.get("https://play.google.com/store/apps/details?id=com.mapmyrun.android2&showAllReviews=true")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[./span[text()='Most relevant']]"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@role='option'][./span[text()='Newest']]"))).click() 

浏览器快照:


谢谢您的回复。当我运行代码时,最后一行出现了一个错误。错误为Indexer错误:列表索引超出范围添加更多时间或开始使用webdriver等待。