如何在 selenium python 中按回车键?
how to press enter in selenium python?
我想在 Instagram 中搜索一个特殊的关键词。比如我要搜索这个词:"fast food"。我可以在搜索框中发送此密钥。但是,当我通过 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
你能帮帮我吗?
而不是。提交()尝试'.send_keys(u'\ue007')'
参见:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.keys
需要更多点击次数:
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')
这很有趣。另一种解决方案更好。知道这一点非常重要,您必须在 Instagram 搜索框中按 2 Enter 键。因此,为了消除 while 循环,您可以使用以下代码:
search_button.send_keys("#fast")
time.sleep(5)
search_button.send_keys(u'\ue007')
search_button.send_keys(u'\ue007')
我想在 Instagram 中搜索一个特殊的关键词。比如我要搜索这个词:"fast food"。我可以在搜索框中发送此密钥。但是,当我通过 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
你能帮帮我吗?
而不是。提交()尝试'.send_keys(u'\ue007')'
参见:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.keys
需要更多点击次数:
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')
这很有趣。另一种解决方案更好。知道这一点非常重要,您必须在 Instagram 搜索框中按 2 Enter 键。因此,为了消除 while 循环,您可以使用以下代码: search_button.send_keys("#fast")
time.sleep(5)
search_button.send_keys(u'\ue007')
search_button.send_keys(u'\ue007')