简体   繁体   English

打开新窗口并在新的widnow上运行javascript

[英]Open new window and run javascript on that new widnow

How would I go about opening a new Google Chrome window with javascript (opener via npm), and then fill out a form on that newly opened window? 我将如何使用javascript打开新的Google Chrome窗口(通过npm打开),然后在新打开的窗口上填写表单?

Currently, I have: 目前,我有:

// 2 arrays for storing account creation info
var opener = require('opener')
var newWindow = opener('URL HERE')

how would I go about attaching to that new window and running code on it to fill out a form? 我将如何附加到新窗口并在其上运行代码来填写表单? I'm running the javascript code via node in the Mac terminal. 我正在通过Mac终端中的节点运行javascript代码。

Any help would be appreciated. 任何帮助,将不胜感激。

To do this, you will have to design your form, and host it where opener can actually access it. 要做到这一点,你必须设计你的表单,并在开启者可以实际访问它的地方托管它。 I've not used opener before, but from what I can see it's job is to open the url you pass as parameter, in a new browser process. 我之前没有使用过opener,但是我可以看到它的工作是在新的浏览器进程中打开你作为参数传递的url。 Nothing more. 而已。

Designing and handling the submission of the form is up to you. 设计和处理表单的提交取决于您。 No magic. 没有魔法。 No shortcuts. 没有捷径。

You can use selenium for browser automation. 您可以使用selenium进行浏览器自动化。 Here is a link to the docs of selenium. 这是硒文档的链接。 http://seleniumhq.github.io/selenium/docs/api/javascript/index.html http://seleniumhq.github.io/selenium/docs/api/javascript/index.html

Here is a code to solve your problem with selenium 这是一个用硒解决问题的代码

var webdriver = require('selenium-webdriver'),
    By = require('selenium-webdriver').By,
    until = require('selenium-webdriver').until;

var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

driver.get('url to form');
var form = driver.findElement(By.css('form'));
var element = form.findElement(By.css('input[type=file]'));
element.sendKeys('/path/to/file.txt');
form.submit();
driver.quit();

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

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