首页 > 解决方案 > 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() });
        });
    });
}

标签: javascriptselenium

解决方案


推荐阅读