Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
like image 732
Shawn Schreier Avatar asked Sep 26 '26 06:09

Shawn Schreier


1 Answers

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)
like image 55
Ian Lesperance Avatar answered Sep 28 '26 21:09

Ian Lesperance