简体   繁体   中英

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?

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.

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. 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. Here is a link to the docs of selenium. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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