如何在 selenium python 中按回车键?

新手上路,请多包涵

我想在 Instagram 中搜索一个特殊的关键词。例如,我想搜索这个词:“快餐”。我可以在搜索框中发送此密钥。但是,当我通过 Python3 在 Selenium 中使用提交方法时,它不起作用并给我错误。这是我的代码:

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

这是错误的:

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

你可以帮帮我吗?

原文由 Hamed Baziyad 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 912
2 个回答

代替。提交()尝试’.send_keys(u’\ ue007’)’

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

原文由 Frank 发布,翻译遵循 CC BY-SA 3.0 许可协议

它需要更多的点击:

 search_button=driver.find_element_by_xpath("""/html/body/span/section/nav/div[2]/div/div/div[2]/input""")

    search_button.send_keys("fast")
    while True:
        search_button.send_keys(u'\ue007')

原文由 Hamed Baziyad 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进