doomdaam
doomdaam

Reputation: 783

Cannot input text into field using Selenium

I keep getting the error: AttributeError: 'NoneType' object has no attribute 'send_keys'

I believe the error happens because I cannot navigate to the field. This is the xpath=

('//*[@id="content"]/div/div[2]/div[2]/form/div[1]/div[1]/div/input')

I cannot seem to get the field.

Here's the html

<div class="containerInput" ng-class="{'error': userSelectedAgeInvalid || userSelectedAgeMissing || userSelectedAgeNotNumber}">
                            <div class="title">Din alder</div>
                            <input afocus="true" tabindex="1" type="text" ng-model="userSelectedAge" ng-blur="timeIt('userSelectedAge')" class="ng-isolate-scope ng-valid ng-dirty">
                            <small ng-show="userSelectedAgeInvalid" class="error alt ng-binding ng-hide">Indtast alder mellem 18 og 120 år</small>
                            <small ng-show="userSelectedAgeMissing" class="error alt ng-hide">Du skal angive en alder</small>
                            <small ng-show="userSelectedAgeNotNumber" class="error alt ng-binding ng-hide">Feltet må kun indeholde tal</small>
                        </div>

And here's my code

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")

# enable browser logging
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'browser':'ALL' } 
driver = webdriver.Chrome(desired_capabilities = d, options=chrome_options)
driver.fullscreen_window()

wait = WebDriverWait(driver,1)

#URL
driver.get("https://forsikringsguiden.dk/#!/bilforsikring/omdig")

#remove cookie bar
driver.find_element_by_id('cookieBarAccept').click()         


#Below is my different attempts. 

#driver.find_element_by_css_selector("input[type='text']").click().send_keys("50")

#wait.until(EC.element_to_be_clickable(By.BycssSelector,'//*[@id="content"]/div/div[2]/div[2]/form/div[1]/div[1]/div')).click().send_keys("50")

element = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="content"]/div/div[2]/div[2]/form/div[1]/div[1]/div/input')))
element.find_element_by_xpath('//*[@id="content"]/div/div[2]/div[2]/form/div[1]/div[1]/div/input').click().send_keys("50")

Upvotes: 0

Views: 51

Answers (1)

SeleniumUser
SeleniumUser

Reputation: 4177

Please try the below code. After finding an element with the visibility_of_all_elements_located you need to specify what action you are going to perform on that element like: sendkey, click

   wait = WebDriverWait(driver, 10)
   element =  wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id="content"]/div/div[2]/div[2]/form/div[1]/div[1]/div/input")))
   element.send_keys("50")

Upvotes: 1

Related Questions