pythonhtmlseleniumkatalon-recorder

Refine Selenium Google search results by time frame and date


I am trying to refine my results after using Selenium and Chrome with python to automate Google searches and get the sorted links. I can successfully get initial search results with the script and automatically click the 'Tools' button.

Bottom line is I cant figure out the required HTML tags to access and select/click the time frame drop down, defaulted to 'Any Time' and then select/click the 'Relevance' drop down to sort by date. I have tried Select but am using the wrong tags for that method. I have used inspect element and Katalon Recorder to figure it out, but I get syntax errors such as "element not found". Any help is appreciated.

driver.get('https://www.google.com/search')
search_field = driver.find_element_by_name("q")

search_field.send_keys("cheese")
search_field.submit()
#  Clicks the Tools button, activates sort dropdowns
driver.find_element_by_id("hdtb-tls").click()

# Need to sort results by last 24, week, month, etc.
driver.find_element_by_class_name('hdtb-mn-hd')
driver.find_element_by_link_text('Past month').click()

# Need to sort results date
driver.find_element_by_xpath('.//*[normalize-space(text()) and normalize- 
space(.)="To"])[1]/following::div[5]')
driver.find_element_by_link_text('Sorted by date').click()

Solution

  • are you missing the .click() for driver.find_element_by_class_name('hdtb-mn-hd')

    driver = webdriver.Chrome()
    
    driver.get('https://www.google.com/search')
    search_field = driver.find_element_by_name("q")
    
    search_field.send_keys("cheese")
    search_field.submit()
    #  Clicks the Tools button, activates sort dropdowns
    driver.find_element_by_id("hdtb-tls").click()
    
    # Need to sort results by last 24, week, month, etc.
    driver.find_element_by_class_name('hdtb-mn-hd').click()
    driver.find_element_by_link_text('Past month').click()
    

    here's a full script that worked it all the way through:

    from selenium import webdriver 
    import time
    
    driver = webdriver.Chrome()
    
    driver.get('https://www.google.com/search')
    search_field = driver.find_element_by_name("q")
    
    search_field.send_keys("cheese")
    search_field.submit()
    #  Clicks the Tools button, activates sort dropdowns
    time.sleep(1)
    driver.find_element_by_id("hdtb-tls").click()
    
    # Need to sort results by last 24, week, month, etc.
    time.sleep(1)
    driver.find_element_by_class_name('hdtb-mn-hd').click()
    time.sleep(1)
    driver.find_element_by_link_text('Past month').click()
    
    # Need to sort results date
    time.sleep(1)
    driver.find_elements_by_xpath('//*[@id="hdtbMenus"]/div/div[3]/div')[0].click()
    time.sleep(1)
    driver.find_elements_by_xpath('//*[@id="sbd_1"]')[0].click()