以下是一个使用Selenium和JavaScript来保持登录会话的示例代码:
const { Builder, By, Key, until } = require('selenium-webdriver');
async function login() {
// 创建一个新的WebDriver实例
let driver = await new Builder().forBrowser('chrome').build();
try {
// 打开登录页面
await driver.get('https://example.com/login');
// 输入用户名和密码,并提交表单
await driver.findElement(By.id('username')).sendKeys('your-username');
await driver.findElement(By.id('password')).sendKeys('your-password', Key.RETURN);
// 等待页面加载完成
await driver.wait(until.titleIs('Home'), 5000);
// 在此处执行其他需要登录会话的操作
// 保持会话的时间,例如30分钟
await driver.sleep(30 * 60 * 1000);
} finally {
// 关闭浏览器并退出会话
await driver.quit();
}
}
login();
这个示例使用Selenium的WebDriver来控制Chrome浏览器,打开登录页面,输入用户名和密码,提交表单,并等待页面加载完成。在此之后,你可以编写其他需要登录会话的代码。在示例中,我们使用driver.sleep()来保持会话的时间,你可以根据自己的需求进行调整。最后,我们关闭浏览器并退出会话。
上一篇:保持dbcontext小