user2293224
user2293224

Reputation: 2220

Python selenium web-driver: selecting value from the dropdown list (No such Element Exception)

I am trying to extract the user reviews from google app using selenium webdriver. I loaded all reviews on the web-browser. Now I want to change the 'Most relevant' option of review page to 'Newest' option as shown in the picture. enter image description here

Here is my code:

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()

When I ran the code, it throws following error:

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']"}

Upvotes: 1

Views: 124

Answers (2)

KunduK
KunduK

Reputation: 33384

To click on google review options as Newest Induce WebDriverWait() and wait for element_to_be_clickable() and following xpath option.

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() 

Browser snapshot:

enter image description here

Upvotes: 1

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

If you want to simply click the div and click newest. Your class is dynamic and will change it's better to get an indentification that won't change.

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()

Upvotes: 1

Related Questions