这是我要提交的表单
<form action="https://www.example.com/account/registration?execution=e2s1" method="post" novalidate="novalidate">
在casperjs文档中的示例代码中,提交表单是这样的:
document.querySelector('form[name="f"]').submit();
如果未指定名称属性,我该如何提交表单?
最佳答案
您需要使用一些 CSS3 选择器或 XPath 选择器来执行此操作。例如,当您打开 Chrome 开发者工具并右键单击一个元素时,您可以选择复制 CSS 路径 和复制 XPath。 CSS 选择器可以与 querySelector
一起使用,但 XPath 选择器只能与 evaluate
一起使用。在页面上下文中。
那些复制的选择器会非常死板,因为它们只能通过使用元素树来工作。您可以选择带有 CSS attribute selector 的表单:
document.querySelector('form[action*="account/registration?execution=e2s1"]').submit();
我在这里使用 *=
因为您可能不希望在选择器中包含域并且协议(protocol) (https) 稍后可能会更改。
如果你只有一种形式,你可以只使用
document.querySelector('form').submit();
或者如果您只有一个具有 novalidate
属性的表单,您可以使用
document.querySelector('form[novalidate]').submit();
关于javascript - 在 casperjs 上提交一个没有名字的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25777827/