Python Seleniu发送_键(键输入)不工作

2024-06-28 11:22:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在在这个网站上https://www.zoocasa.com/ 我正在尝试搜索一个城市,然后按enter键将其加载到下一页,但它不起作用

到目前为止,这就是我的代码中的内容

inputElement = self.driver.find_element_by_xpath('/html/body/div/div[1]/div[3]/div[1]/div/div[1]/input')
for character in 'Toronto':
    inputElement.send_keys(character)
    time.sleep(0.1)
inputElement.send_keys(Keys.ENTER)

我还试过Keys.RETURN,但似乎效果不太好。任何帮助都会很好

编辑:如果你试图访问该网站,键入一个城市,然后按ENTER键,它就会工作


Tags: 代码httpsdivcomsend内容网站www
1条回答
网友
1楼 · 发布于 2024-06-28 11:22:56

不要按“回车”键,但要更具体,然后“单击”搜索按钮。 例如

# Might be worth importing this:
from selenium.common.exceptions import NoSuchElementException
# Tries to click the button
try:
    search_button = browser.find_elements_by_xpath("//*[@id='__next']/div[1]/div[3]/div[1]/div/div[1]/div[2]/button[2]")[0]
    search_button.click()
    # If you see this in console, the button has been clicked
    print("Button clicked")
except NoSuchElementException:
    # If you see this, the button has not been found / clicked
    print("No button found")

相关问题 更多 >