Python selenium 网络驱动程序:从下拉列表中选择值(没有这样的元素异常)
Python selenium web-driver: selecting value from the dropdown list (No such Element Exception)
我正在尝试使用 selenium webdriver 从 google 应用中提取用户评论。我在网络浏览器上加载了所有评论。现在我想将评论页面的 'Most relevant' 选项更改为 'Newest' 选项,如图所示。
这是我的代码:
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 并单击最新的。您的 class 是动态的并且会改变,最好获得不会改变的标识。
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()
点击 google review options
作为 Newest
诱导 WebDriverWait
() 并等待 element_to_be_clickable
() 和以下 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()
浏览器快照:
我正在尝试使用 selenium webdriver 从 google 应用中提取用户评论。我在网络浏览器上加载了所有评论。现在我想将评论页面的 'Most relevant' 选项更改为 'Newest' 选项,如图所示。
这是我的代码:
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 并单击最新的。您的 class 是动态的并且会改变,最好获得不会改变的标识。
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()
点击 google review options
作为 Newest
诱导 WebDriverWait
() 并等待 element_to_be_clickable
() 和以下 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()
浏览器快照: