Python 3.x 如何在selenium python中按enter键?
我想在Instagram中搜索一个特殊的关键字。例如,我想搜索这个词:“快餐”。我可以在搜索框中发送此密钥。但是,当我使用Python3在Selenium中使用submit方法时,它不起作用,并给我带来错误。这是我的代码:Python 3.x 如何在selenium python中按enter键?,python-3.x,selenium,submit,instagram,enter,Python 3.x,Selenium,Submit,Instagram,Enter,我想在Instagram中搜索一个特殊的关键字。例如,我想搜索这个词:“快餐”。我可以在搜索框中发送此密钥。但是,当我使用Python3在Selenium中使用submit方法时,它不起作用,并给我带来错误。这是我的代码: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.alert import Alert from sele
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
你能帮我吗?代替。submit()尝试发送密钥(u'\ue007')'
请参阅:需要更多的点击:
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')
这很有趣。另一个解决方案更好。知道这一点非常重要,您必须按2键进入Instagram搜索框。因此,为了消除while循环,可以使用以下代码: 搜索按钮。发送按键(“快速”)
您能提供相关的HTML吗?在哪一行代码上给出了异常。我没有看到带有异常中提到的定位器的代码行。它比以前的代码更完整。需要使用xpath吗?它不起作用。只有错误被删除,搜索按钮没有按您的解决方案与我的方法类似:搜索按钮。发送按键(“快速”,按键。返回)在你的问题中分享你已经试图解决的问题是一个很好的做法。这里有很多关于不同语言SendKeys问题的信息:我可以解决它,我在回答框中分享我的回答
time.sleep(5)
search_button.send_keys(u'\ue007')
search_button.send_keys(u'\ue007')