以上所有内容都是旧的,您现在可以使用selenium-webdriver
。就我而言,我只是忘记了包含By
,我的 IDE 没有帮助。
ReferenceError: By is not defined
我只需要:
const {By} = require('selenium-webdriver');
截至 02022020 的完整示例,包括使用 chromedriver 设置路径!
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const chromedriver = require('chromedriver');
const { By, Key } = webdriver; // Using object destructuring for convenience - or webdriver.By
chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
const driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
driver.get('https://www.google.com');
driver.findElements(By.xpath('//input[@type="text"]'))
// ES6 desturcturing usage. Could be: (inputs), and then inputs[0].sendKeys...
.then(([searchInput]) => {
// inputs[0].sendKeys(...words);
searchInput.sendKeys('stack overflow bumper stickers')
.then(() => {
searchInput.sendKeys(Key.ENTER);
});
});