Search code examples
python-3.xseleniumsubmitinstagramenter

how to press enter in selenium python?


I want to search a special keyword in Instagram. For example, I want to search this word:"fast food". I can send this key in search box. But, when I use submit method in Selenium by Python3, it doesn't work and give me error. This is my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Firefox()
url="https://www.instagram.com/p/pTPI-kyX7g/?tagged=resurant"
driver.get(url)

#login_button=driver.find_element_by_xpath("""/html/body/span/section/main/article/div[2]/div[2]/p/a""")
#login_button.click()
import time
driver.implicitly_wait(5)
search_button=driver.find_element_by_xpath("""/html/body/span/section/nav/div[2]/div/div/div[2]/input""")
search_button.send_keys("fast food")
search_button.submit()

This is gave error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: ./ancestor-or-self::form

Could you help me?


Solution

  • Instead of. submit() try '.send_keys(u'\ue007')'

    See: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.keys