Search code examples
pythonseleniumweb-scrapingselenium-chromedriversendkeys

Why am I not able to use sendkeys to enter text into textbox?


I am trying to enter text on this webpage: https://finra-markets.morningstar.com/BondCenter/BondTradeActivitySearchResult.jsp?ticker=C679131&startdate=10%2F03%2F2019&enddate=10%2F03%2F2020

This is the code that I have so far

from selenium import webdriver

import os

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC



chrome_driver = os.path.abspath('C:/Users/ross/Desktop/chromedriver.exe')

browser = webdriver.Chrome(chrome_driver)

browser.get('https://finra-markets.morningstar.com/BondCenter/BondTradeActivitySearchResult.jsp?ticker=C679131&startdate=10%2F03%2F2019&enddate=10%2F03%2F2020')

#clicks agreement to morningstar's terms of service
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#ms-agreement > input'))).click()

#tries to clear text box at bottom right hand side of page
browser.find_element_by_class_name('qs-ui-ipt qs-pageutil-input').clear()

#tries to enter '2' in text box
browser.find_element_by_class_name('qs-ui-ipt qs-pageutil-input').send_keys(2)

I am trying to enter the number '2' into the text box that is at the bottom right of the chart.

This is the error message I receive when I run the code listed above: 'Message: no such element: Unable to locate element: {"method":"css selector","selector":".qs-ui-ipt qs-pageutil-input"}'

I see most tutorials use 'find_element_by_id' to run this process but the element on the web page doesn't have an ID associated with it. I'm not sure if I need an ID or not to execute this process but I thought I might as well share. I also tried to find the element by XPath and by CSS selector but it didn't work.

Any and all help is much appreciated!

Thanks, Ross


Solution

  • Use WebDriverWait() and wait for element_to_be_clickable() and following css selector.

    driver.get("https://finra-markets.morningstar.com/BondCenter/BondTradeActivitySearchResult.jsp?ticker=C679131&startdate=10%2F03%2F2019&enddate=10%2F03%2F2020")
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.button_blue.agree"))).click()
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.qs-ui-ipt.qs-pageutil-input"))).clear()
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.qs-ui-ipt.qs-pageutil-input"))).send_keys("2")
    

    Browser snapshot:

    enter image description here