简体   繁体   中英

I can't solve this problem can someone help me? “'NoneType' object has no attribute 'send_keys'”

The problem is how can i use send_keys? Because it is not writing in the search bar.

I search in the documents but i can't solve it.

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)

The error what is giving is:

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

You assign wrong value to searchbtn

You assing value returned by click() which is always None

You have to do it in two steps

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

and then searchbtn is correct and you can use send_key()


After this change code works

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)

My first thought is that you rather have an issue with the searchbtn object ( NoneType ). Are you sure select2-search__field exists?

please try below solution

you were trying to send data to wrong element select2-search__field present on UI. instead of that we need to identify //input[@class='select2-search__field'] element and then we can send data for search.

    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:\chromedriver.exe')

    # 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()
    driver.find_element_by_class_name("select2-search__field").click()

    driver.find_element_by_xpath("//input[@class='select2-search__field']").send_keys('Amsterdam')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM