bensw
bensw

Reputation: 3028

How to select a value from a drop-down using Selenium from a website with special setting- Python

Note: I particularly deal with this website

How can I use selenium with Python to get the reviews on this page to sort by 'Most recent'?

What I tried was:

driver.find_element_by_id('sort-order-dropdown').send_keys('Most recent') 

from this didn't cause any error but didn't work.

Then I tried

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('sort-order-dropdown'))
select.select_by_value('recent')
select.select_by_visible_text('Most recent')
select.select_by_index(1)

I've got: Message: Element <select id="sort-order-dropdown" class="a-native-dropdown" name=""> is not clickable at point (66.18333435058594,843.7999877929688) because another element <span class="a-dropdown-prompt"> obscures it

This one

element = driver.find_element_by_id('sort-order-dropdown')
element.click()
li = driver.find_elements_by_css_selector('#sort-order-dropdown > option:nth-child(2)')
li.click()

from this caused the same error msg

This one from this caused the same error also

Select(driver.find_element_by_id('sort-order-dropdown')).select_by_value('recent').click()

So, I'm curious to know if there is any way that I can select the reviews to sort from the most recent first.

Thank you

Upvotes: 1

Views: 1391

Answers (2)

Eugene S
Eugene S

Reputation: 6910

This worked for me using Java:

@Test
public void amazonTest() throws InterruptedException {
    String URL = "https://www.amazon.com/Harry-Potter-Slytherin-Wall-Banner/product-reviews/B01GVT5KR6/ref=cm_cr_dp_d_show_all_top?ie=UTF8&reviewerType=all_reviews";
    String menuSelector = ".a-dropdown-prompt";
    String menuItemSelector = ".a-dropdown-common .a-dropdown-item";

    driver.get(URL);

    Thread.sleep(2000);

    WebElement menu = driver.findElement(By.cssSelector(menuSelector));
    menu.click();

    List<WebElement> menuItem = driver.findElements(By.cssSelector(menuItemSelector));
    menuItem.get(1).click();
}

You can reuse the element names and follow a similar path using Python.

The key points here are:

  1. Click on the menu itself
  2. Click on the second menu item

It is a better practice not to hard-code the item number but actually read the item names and select the correct one so it works even if the menu changes. This is just a note for future improvement.

EDIT This is how the same can be done in Python.

import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

URL = "https://www.amazon.com/Harry-Potter-Slytherin-Wall-Banner/product-reviews/B01GVT5KR6/ref=cm_cr_dp_d_show_all_top?ie=UTF8&reviewerType=all_reviews";
menuSelector = ".a-dropdown-prompt";
menuItemSelector = ".a-dropdown-common .a-dropdown-item";

driver = webdriver.Chrome()
driver.get(URL)

elem = driver.find_element_by_css_selector(menuSelector)
elem.click()

time.sleep(1)
elemItems = []
elemItems = driver.find_elements_by_css_selector(menuItemSelector)
elemItems[1].click()

time.sleep(5)
driver.close()

Just to keep in mind, css selectors are a better alternative to xpath as they are much faster, more robust and easier to read and change.

Upvotes: 1

bensw
bensw

Reputation: 3028

This is the simplified version of what I did to get the reviews sorted from the most recent ones. As "Eugene S" said above, the key point is to click on the button itself and select/click the desired item from the list. However, my Python code use XPath instead of selector.

# click on "Top rated" button
driver.find_element_by_xpath('//*[@id="a-autoid-4-announce"]').click() 
# this one select the "Most recent"
driver.find_element_by_xpath('//*[@id="sort-order-dropdown_1"]').click() 

Upvotes: 0

Related Questions