SirAchesis
SirAchesis

Reputation: 345

Selenium not sending keys to input field

I'm trying to scrape this url https://www.veikkaus.fi/fi/tulokset#!/tarkennettu-haku

There's three main parts to the scrape:

  1. Select the correct game type from "Valitse peli"
    For this I want to choose "Eurojackpot"
  2. Set the date range from variables. In the full version I'll be generating dates based on the 12 week range limit. For now I've just chose two dates that are close enough. This date range needs to be inputted into the two input fields below "Näytä tulokset aikaväliltä"
  3. I need to click the show results button. (Labeled "Näytä Tulokset")

I believe my code does parts 1 and 3 correct, but I'm having trouble with part 2. For some reason the scraper isn't sending the dates to the elements. I've tried click, clear and then send_keys. I've also tried to first send key_down(Keys.CONTROL) then send_keys("a") and then send_keys(date), but none of these are working. The site always goes back to the date it loads up with (current date).

Here's my full code:

# -*- coding: utf-8 -*-
"""
Created on Sat Jun 12 12:05:40 2021

@author: Samu Kaarlela
"""
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options

url = "https://www.veikkaus.fi/fi/tulokset#!/tarkennettu-haku"
wd = r"C:\Users\Oppilas\Desktop\EJ prediction\scraper\chromedriver"

chrome_options = Options()
chrome_options.add_argument("--headless")

webdriver = webdriver.Chrome(
    wd,
    options=chrome_options
)

from_date = "05.05.2021"
to_date = "11.06.2021"
    
with webdriver as driver:
    wait = WebDriverWait(driver,10)
    driver.get(url)
    
    game_type_element = driver.find_element_by_css_selector(
        "#choose-game"
    )
    slc = Select(game_type_element)
    slc.select_by_visible_text("Eurojackpot")
    
    
    from_date_element = WebDriverWait(
        driver,
        20).until(
            EC.element_to_be_clickable(
                (
                    By.CSS_SELECTOR,
                    "#date-range div:nth-child(1) input"
                )
        )
    )
            
    
    ActionChains(driver). \
    click(from_date_element). \
    key_down(Keys.CONTROL). \
    send_keys("a"). \
    send_keys(from_date). \
    perform()
    
    print(from_date_element.get_attribute("value"))
    driver.save_screenshot("./image.png")
    driver.close()
    

EDIT:

I just realized that when selected the input field goes from #date-range #from-date to #date-range #from-date #focus-visible

Upvotes: 0

Views: 104

Answers (1)

C. Peck
C. Peck

Reputation: 3711

For me, simply doing the following works:

driver.find_element_by_css_selector('.date-input.from-date').send_keys(from_date)
ActionChains(driver).send_keys(Keys.RETURN).perform()
driver.find_element_by_css_selector('.date-input.to-date').send_keys(to_date)
ActionChains(driver).send_keys(Keys.RETURN).perform()

Upvotes: 1

Related Questions