硒在链接后获取元素

时间:2018-11-29 08:38:58

标签: 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() });
        });
    });
}

0 个答案:

没有答案