javascript 以编程方式单击另一个网页中的输入按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6685550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
programmatically click an input button in another web page?
提问by Chuck Han
I know there have been several similar questions, but I haven't seen an answer to my specific need: Is there a way to click a button in a separately launched web page? For example, I launch another web page via:
我知道有几个类似的问题,但我还没有看到满足我特定需求的答案:有没有办法在单独启动的网页中单击按钮?例如,我通过以下方式启动另一个网页:
<a href="x" target="y">
orwindow.open()
<a href="x" target="y">
或者window.open()
Can I then click an input button in that launched web page programmatically?
然后我可以以编程方式单击该启动网页中的输入按钮吗?
回答by MoarCodePlz
Unless the page is underneath your own control or resides on the same domain then no, it is not possible. This would be cross-site scripting and all browsers have security sandboxes in place to keep something like this from happening. Why are you trying to programmatically press a button on a page that you're also programmatically opening?
除非页面在您自己的控制之下或驻留在同一个域中,否则不,这是不可能的。这将是跨站点脚本,并且所有浏览器都有适当的安全沙箱来防止类似的事情发生。您为什么要尝试以编程方式按下您也以编程方式打开的页面上的按钮?
回答by ysrb
Yes. When you do window.open it will return you a window object. Var win = window.open(); win.fnSubmit(); assuming fnSubmit is the function on the other page that will do the.clicking. and both pqges on the same domain.
是的。当你执行 window.open 时,它会返回一个 window 对象。var win = window.open(); win.fnSubmit(); 假设 fnSubmit 是另一个页面上的函数,它将执行.clicking。并且两个 pqges 在同一个域上。
回答by amelvin
This is a technique used by some hacking injection attacks. Basically you can inject javascript into the querystring that can attach itself to the DOM, change an image or swf file source or simply run when the page is loaded; example hereand example here.
这是一些黑客注入攻击使用的技术。基本上,您可以将 javascript 注入到查询字符串中,该字符串可以将自身附加到 DOM、更改图像或 swf 文件源或在页面加载时简单地运行;示例here和示例here。
Or if you already know the structure of the other page you can directly target methods or objects.
或者,如果您已经知道其他页面的结构,您可以直接定位方法或对象。
But as these are not nice things I assume that you have good reasons why you can't touch the code on the receiving page but want to adjust its behaviour?
但由于这些都不是好事,我假设您有充分的理由为什么不能触摸接收页面上的代码但想要调整其行为?
回答by cwallenpoole
Survey says... maybe. If you've done your homework, you will probably be able to get communication to happen within the same subdomain. It gets progressively more difficult from there -- I've never had consistent cross-browser sub-domain support for manipulating JavaScript between two windows (or iframes).
调查说……也许吧。如果您完成了作业,您可能能够在同一个子域内进行通信。从那里开始变得越来越困难——我从来没有一致的跨浏览器子域支持在两个窗口(或 iframe)之间操作 JavaScript。
That said, if they are the same domain, it is a matter of descending into it. If I have:
也就是说,如果它们是同一个域,那就是下降到它的问题。如果我有:
- Window A opens Window B
- Window B declares
var winBVar
- Window A is able to access winBVar after, and only after, that variable is declared (as in, you still need to wait for document.onload, etc)
- 窗口 A 打开窗口 B
- B窗声明
var winBVar
- 在声明该变量之后,窗口 A 才能访问 winBVar(例如,您仍然需要等待 document.onload 等)
回答by zzzzBov
You can click on any element you can select.
您可以单击可以选择的任何元素。
You can only select elements in windows that you have security privelages to.
您只能在具有安全权限的窗口中选择元素。
You have rights to your own window, and rights to windows within the same domain & security.
您拥有自己的窗口的权利,以及同一域和安全性内的窗口的权利。
To access the element you'll need the window object, which is returned from window.open
要访问该元素,您将需要 window 对象,该对象从 window.open
var newWin = window.open(siteYouHaveAccessTo);
newWin.document.getElementById('foo').click();
If you're trying to click on the Search
button on www.google.com
, you're SOL.
如果您尝试单击 上的Search
按钮www.google.com
,那么您就是 SOL。