1

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

Eugene S
  • 6,709
  • 8
  • 57
  • 91
bensw
  • 2,818
  • 5
  • 21
  • 31
  • Your first link is broken – Eugene S Dec 15 '17 at 05:44
  • My apologises. It's been fixed. – bensw Dec 15 '17 at 06:03
  • Not sure I understand what drop down menu are you referring to. For me, in order to get to the drop down menu, I had first to select "See all 71 customer reviews. Is this the case or I am missing something? – Eugene S Dec 15 '17 at 06:47
  • Possible duplicate of [Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-36-72-other-el) – undetected Selenium Dec 15 '17 at 06:58
  • @Eugene S yes, I wanted to sort from that page. Sorry for the confusion. Also updated in the thread. – bensw Dec 15 '17 at 07:05
  • @DebanjanB that one used java while I use python but will have a look if it helps. thx. – bensw Dec 15 '17 at 07:07
  • @bensw The concept and JS commands remains the same. Only the coding style varies. – undetected Selenium Dec 15 '17 at 07:09
  • So do you actually navigate there? Or in other words, do you click on the "*See all 71 customer reviews*" and then you have the issue you are describing? – Eugene S Dec 15 '17 at 07:18
  • @Eugene S Yes, I did click on the *"See all 71 customer reviews"* and encountered that issue when I made an attempt to sort the reviews. – bensw Dec 15 '17 at 07:22
  • @Eugene S Been trying to update the link but it won't stay. Very bizarre. – bensw Dec 15 '17 at 07:25
  • @Eugene Found the workaround but if you want the bounty go ahead, otherwise will delete this thread. – bensw Dec 15 '17 at 07:32
  • Better post the workaround so the others can learn. If I find there is a better way to do that, I will post an additional answer. – Eugene S Dec 15 '17 at 07:35

2 Answers2

1

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.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
  • Thank you for the additional remark. Appreciate that. Tried to translate your code to Python but no luck so far. Thank you for being around though. – bensw Dec 15 '17 at 08:40
  • @bensw I added a Python version. Just keep in mind, css selectors are a better alternative to xpath as they are much faster, more robust and easier to read and change. – Eugene S Dec 16 '17 at 01:22
  • 1
    @ Eugene S That worked. I'll accept your answer as a correct answer. – bensw Dec 16 '17 at 13:47
0

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() 
bensw
  • 2,818
  • 5
  • 21
  • 31