Skip to content
Advertisement

Selenium error: Message: Select only works on elements, not on

I know that I should find “select” element so I can choose from drop-down list, But here google trends don’t provide “select” element and I want to choose any value from data and time list and When I try to do this I got this error ‘Message: Select only works on elements, not on ‘…. I found a solution but It was written by ‘requests’ module and I want to use selenium

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

from selenium.webdriver.support.ui import Select
import time

driver = webdriver.Chrome(executable_path="C:\Drivers\chromedriver.exe")


driver.get("https://trends.google.com/trends")

key1 = driver.find_element_by_xpath('//*[@id="sidenav-menu-btn"]/div')
key1.click()
time.sleep(1)

key2 = driver.find_element_by_xpath('//*[@id="sidenav-list-group-trends"]/md-item[2]/md-item-content/a/i')
key2.click()
time.sleep(2)


x = driver.find_element_by_xpath('//*[@id="select_12"]')
x.click()

drp = Select(x)
drp.select_by_index(2)

Advertisement

Answer

Just grab them by the id directly, instead of using select

drp = driver.find_element_by_id("select_option_22") # equates to 'Past 5 years'
drp.click()

That opened up the Past 5 years (using that unique id) when clicked

drp = driver.find_element_by_id("select_option_20") # equates to 'Past 30 days'

Would be the Past 30 days menu option, and so on….

Or, you can go for the text directly…

drp = driver.find_element_by_xpath("//div[normalize-space(text())='Past 7 days']")
drp.click()

Note the normalize-space, this is used to strip the white space which this element text value has. It won’t work without it.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement