使用 Mechanicalsoup 设置没有名称的表单元素的值 [英] Using mechanicalsoup to set value of form element w/o a name
问题描述
我已经搜索了所有的机械汤&Beautifulsoup 文档,但无法弄清楚如何使用 'id' 设置表单元素的值(因为它没有名称).
I have searched through all mechanicalsoup & beautifulsoup documentation but can't figure out how to set the value of a form element using 'id' (because it doesn't have a name).
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open(my_url)
form = browser.select_form('form[id="login-form"]')
browser.get_current_form().print_summary()
userid = browser.get_current_page().find('input', id='text-userid')
form.set("text-userid", "user")
这让我 -
<input class="login-text-box" id="text-userid" placeholder="Email" type="text" value=""/>
<input class="login-text-box" id="text-password" placeholder="Password" type="password" value=""/>
<input id="button-login" type="submit" value="Sign In"/>
<input id="remember-me-checkbox" name="rememberme" type="checkbox" value="rememberme"/>
LinkNotFoundError: No valid element named text-userid
我尝试通过 id、CSS 选择器和其他组合来引用元素,但得到了相同的错误.我能够通过以下方式获取实际标签(我发现这是提交按钮类似问题的解决方案):
I have tried referring to the element via id, CSS selector, and other combinations but get the same error. I am able to get the actual tag (which I found as a solution to a similar problem with submit buttons) with:
userid = browser.get_current_page().find('input', id='text-userid')
但 form.set() 不直接接受标签.
but form.set() doesn't accept tags directly.
感谢您的帮助!
推荐答案
经过更多研究,我了解到我应该在这种情况下使用 Selenium(而不是机械汤).我使用了本指南,它在第一次尝试时完全满足了我的需求.
After more research, I learned I should be using Selenium for this case (not mechanicalsoup). I used this guide which did exactly what I needed on the first try.
这篇关于使用 Mechanicalsoup 设置没有名称的表单元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!