Python - How to get Selenium send_keys to work in IE11

Viewed 293

I am building a web scraper to download PDF files from a public school database site, and the script itself works fine with Chrome, which is what I would prefer. However, the site will ONLY allow you to use IE to download files. The script uses Selenium and I've been trying to use the IE driver (both 32 and 64 bit) to no avail. It opens the webpage, but it inputs nothing when I use the send_keys method and I receive no errors. Again, this works fine in Chrome. Here's the code:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
import pandas as pd
url = "https://mdoe.state.mi.us/cms/grantauditorreport.aspx"
districtlist = pd.read_csv(filepath_or_buffer=r'DistrictList.csv',squeeze=True)

for district in districtlist:
ieoptions = webdriver.IeOptions()
driver = webdriver.Ie(executable_path='IEDriverServer.exe',options=ieoptions)
driver.get(url)

search = driver.find_element_by_name("ctl00$cphMain$txtAgency")
search.send_keys('%')
driver.find_element_by_css_selector('.ButtonStandard').click()

select = Select(driver.find_element_by_id('ctl00_cphMain_ddlAgency'))

select.select_by_visible_text(district)

start_date = driver.find_element_by_name("ctl00$cphMain$txtBeginDate")
driver.execute_script("arguments[0].value = ''", start_date)
start_date.send_keys('4/2/2018')
end_date = driver.find_element_by_name("ctl00$cphMain$txtEndDate")
driver.execute_script("arguments[0].value = ''", end_date)
end_date.send_keys('4/9/2021')

driver.find_element_by_name('ctl00$cphMain$btnSearch').click()

time.sleep(10)

driver.quit()
quit()

I'm new to Python and this is my first foray into web scraping or Selenium.

Thanks!

2 Answers

First, add waits:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input[name="ctl00$cphMain$txtAgency"]')))

Then try:

search = driver.find_element_by_css_selector('input[name="ctl00$cphMain$txtAgency"]')
search.click()
search.send_keys('%')

Or,

search = driver.find_element_by_css_selector('input[name="ctl00$cphMain$txtAgency"]')
search.clear()
search.send_keys('%')

IE is always pain to automate.

Your code is right. I test and find that if we run the python code as administrator, it can run well in IE.

You can first run cmd as administrator like below and then navigator to the path of your py file and run the command:

python yourfile.py

enter image description here

Related