我无法解决此问题,有人可以帮助我吗? “'NoneType'对象没有属性'send_keys'”

时间:2019-10-28 23:06:36

标签: python selenium selenium-webdriver selenium-chromedriver

问题是我该如何使用send_keys?因为它没有写在搜索栏中。

我搜索了文档,但无法解决。

from selenium import webdriver 
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 
import time 

# Open Chrome
driver = webdriver.Chrome('C:/xampp/htdocs/pegasus/chromedriver') 

# Going to website
driver.get("https://www.flypgs.com/en")

# Select button by class name and click on it.
frombtn = driver.find_element_by_class_name('select2-selection').click()
searchbtn = driver.find_element_by_class_name("select2-search__field").click()

# ERROR is here below on send_keys.
searchbtn.send_keys('Amsterdam')

time.sleep(1000000)

给出的错误是:

Exception has occurred: AttributeError
'NoneType' object has no attribute 'send_keys'
File "C:\xampp\htdocs\pegasus\app.py", line 17, in <module>
searchbtn.send_keys('Amsterdam')

1 个答案:

答案 0 :(得分:1)

您为searchbtn分配了错误的值

您要求click()返回的值始终为None

您必须分两步进行

searchbtn = driver.find_element_by_class_name("select2-search__field")
searchbtn.click()

然后searchbtn是正确的,您可以使用send_key()


此更改代码生效后

from selenium import webdriver 
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 
import time 

driver = webdriver.Chrome('C:/xampp/htdocs/pegasus/chromedriver') 

driver.get("https://www.flypgs.com/en")

frombtn = driver.find_element_by_class_name('select2-selection')
frombtn.click()

searchbtn = driver.find_element_by_class_name("select2-search__field")
searchbtn.click()

searchbtn.send_keys('Amsterdam')

time.sleep(1000000)