简体   繁体   English

不能将 submit() 与 Selenium (Python) 一起使用

[英]Can't use submit() with Selenium (Python)

I have used driver.find_element_by_id('SearchProductName') to find the element with the following HTML:我使用driver.find_element_by_id('SearchProductName')来查找具有以下 HTML 的元素:

<input autocomplete="off" name="SearchProductName" id="SearchProductName"
class="sg-input headerSearchBox ac_input" placeholder="Search for a 
product" tabindex="0">

It has recognised this element correctly and I have been able to send_keys to it, but when I try to submit() , I get the following error.它已正确识别此元素,并且我已经能够将其send_keys给它,但是当我尝试submit() ,出现以下错误。

selenium.common.exceptions.NoSuchElementException: Message: 
Element was not in a form so couldn't submit

I don't understand how if the element is a textbox which I can send_keys to, it is not then a form which I can submit.我不明白如果元素是我可以将send_keys发送到的文本框,那么它就不是我可以提交的表单。 How do I fix this problem so that I use submit on this element?我如何解决这个问题,以便我在这个元素上使用提交?

.submit() is intended for controls that are inside of a FORM . .submit()用于FORM内部的控件。 It's a shortcut way to submit the form without clicking the Submit button.这是一种无需单击“提交”按钮即可提交表单的快捷方式。 It looks like in your case the INPUT is not inside a FORM tag, thus the error message.在您的情况下,看起来INPUT不在FORM标记内,因此是错误消息。 I don't know what your page looks like but do with code what a user would do.我不知道你的页面是什么样子,但用代码做用户会做什么。 The user fills out the input... what's their next step?用户填写输入...下一步是什么? Click a button or ?单击按钮或 ? Do that with code and you should be able to progress in your scenario.用代码做到这一点,你应该能够在你的场景中取得进展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM