无法使用Selenium将文本输入字段

时间:2019-10-29 09:28:32

标签: python-3.x selenium

我不断收到错误消息: AttributeError:“ NoneType”对象没有属性“ send_keys”

我相信会发生错误,因为我无法导航至该字段。 这是xpath =

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

我似乎无法获得这个领域。

这是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>

这是我的代码

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")

1 个答案:

答案 0 :(得分:1)

请尝试以下代码。在使用visibility_of_all_elements_located查找元素之后,您需要指定要对该元素执行的操作,例如: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")