javascript - Selenium 跟随链接后获取元素
问题描述
全部!我已经尽可能多地遵循了提示和教程,但是从起始页面移动后,我无法弄清楚如何在 Node JS Selenium 中获取元素。
在我的用例中,我进入登录屏幕,输入相关信息,然后在登录按钮上运行 click()。我被带到主屏幕。
在此之后,我无法选择任何元素!我只是不断收到“未找到”警告。
我睡了 6000 毫秒,我已将隐式超时设置为 30 秒。
我在这里想念什么?我有问题的部分将在函数 GoToSettings 中,该函数在单击登录按钮后调用。
谢谢
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
const TIMEOUT = 30
var driver = new webdriver.Builder().forBrowser('chrome').build();
const capabilities = driver.getCapabilities().then(()=>{
capabilities['map_'].set('timeouts', { implicit: TIMEOUT, pageLoad: TIMEOUT, script: TIMEOUT });
});
var info = require('./info.json')
driver.get('http://www.okcupid.com/settings?')
driver.sleep(2000);
//We may already be logged in at this point
driver.getTitle().then((title) => {
driver.getCurrentUrl().then((url) => {
LoginOrHome(title, url);
});
});
function LoginOrHome(title, url){
if(title == "Free Online Dating | OkCupid" || url == "https://www.okcupid.com/login?p=/settings") {
//needslogin.exe
console.log("Logging in!");
login();
} else if (title == "Welcome! | OkCupid" || url == "https://www.okcupid.com/settings?") {
//We are already logged in
console.log("Already logged in. Changing location.");
goToSettings();
} else {
console.log(title);
console.log(url);
}
}
function goToSettings() {
driver.sleep(6000);
driver.switchTo().window(driver.getWindowHandle()).then( () => {
driver.getCurrentUrl().then((title) => {
//This prints the URL of the previous page
console.log(title);
});
})
}
function login(){
driver.findElement(By.name("username")).sendKeys(info.email).then(() => {
driver.findElement(By.name("password")).sendKeys(info.password).then(() => {
driver.findElement(By.css("div > input[type=submit]")).click().then(()=> { goToSettings() });
});
});
}
解决方案
推荐阅读
- visual-studio - 如何在 Windows server 2012 中安装没有 Visual Studio 2015 的 Crystal Report?
- sql-server - 如何按天和小时字段进行排序?
- listview - React Native 中的平滑动画滚动视图
- javascript - 如何使用Nodejs查找每个数组对象的位置
- excel - 为文本列分配一个新的唯一 int 列
- jquery - 修复了 jQuery 可排序列表中的项目
- angularjs - 服务器端框架与客户端框架
- pubnub - 发布访问拒绝异常。错误数据=PNErrorData(信息=空
- node.js - 由于节点 js 内存不足,NPM 命令显示中止消息
- xcode - 重新编译没有沙箱的旧可可应用程序