如何在selenium登录node.js后获取其他页面?

时间:2018-03-16 02:39:06

标签: node.js selenium

我想抓取大学主页的分数。

所以我使用了selenium并成功登录主页。

但登录后我无法获得“记分板页面”。

我能做什么?

var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var driver = new webdriver.Builder().forBrowser('chrome').build();

var hisnet = "https://hisnet.handong.edu/login/login.php";
driver.get(hisnet);

var By = webdriver.By;

driver.findElement(By.name('id')).sendKeys('something');
driver.sleep(4000);
driver.findElement(By.name('password')).sendKeys('something');
driver.sleep(4000);
driver.findElement(By.xpath('//input[@ src="/2012_images/intro/btn_login.gif"]')).click();

// below code is not working as my think.
driver.sleep(10000);
driver.get("https://hisnet.handong.edu/haksa/record/HREC110M.php");

,错误消息在

下面
(node:16184) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"*[name="id"]"}
  (Session info: chrome=65.0.3325.162)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64)
(node:16184) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:16184) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@ src="/2012_images/intro/btn_login.gif"]"}
  (Session info: chrome=65.0.3325.162)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64)
(node:16184) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"*[name="password"]"}
  (Session info: chrome=65.0.3325.162)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64)

2 个答案:

答案 0 :(得分:0)

尝试一下:

process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
  // application specific logging, throwing an error, or other logic here
});

答案 1 :(得分:-1)

您可以使用driver.wait代替driver.sleep来重试。

您可以参考本文here。希望对您有所帮助。