Shawn Schreier
Shawn Schreier

Reputation: 904

Cannot focus on element - Selenium, Chrome, Python

I am trying to go to the drought monitor website and tell it to select to show county data. I am able to get my code to navigate to the website, and it clicks the dropdown, but I cannot get it to type in "county". My code gets to the last line and then give the error: "Cannot focus element".

Any help would be greatly appreciated as I'm very new to Selenium.

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()
browser.get('http://droughtmonitor.unl.edu/Data/DataDownload/ComprehensiveStatistics.aspx')

browser.maximize_window()
dropdown = browser.find_element_by_xpath("""//*
[@id="dnn_ctr1009_USDMservice_CompStats_2017_aoiType_chosen"]""")
dropdown.click()
dropdown.send_keys('county')
dropdown.submit()
print("I'm done")

Upvotes: 3

Views: 15807

Answers (1)

Ian Lesperance
Ian Lesperance

Reputation: 5139

You're sending keys to the <div> that contains the search <input>, rather than to the <input> element itself. You'll need to find the <input> and send it the keys.

(Note: You also don't need to use XPath for something as simple as a lookup by id.)

dropdown = browser.find_element_by_id("dnn_ctr1009_USDMservice_CompStats_2017_aoiType_chosen")
dropdown.click()
search = dropdown.find_element_by_tag_name("input")
search.send_keys("county", Keys.ENTER)

Upvotes: 7

Related Questions