如何使用Selenium登录需要用户名和密码的站点?

时间:2011-04-07 06:47:40

标签: python selenium

我正在使用selenium api和python开发自动化测试脚本。但是当我从selenium rc运行脚本时,它会进入登录页面。如何在该页面上输入我的用户名和密码,因为它不包含任何会话或cookie?

2 个答案:

答案 0 :(得分:8)

以下是Adam Goucher在Selenium IRC频道上向我提出的建议(irc://irc.freenode.net/selenium):

利用完全干净的浏览器缓存/ cookie历史记录的情况。对您的测试进行编码以进行登录。

然而,不是在代码中硬编码用户名和密码,而是在客户端本地保存一个故意非源代码修订的配置文件,用户名和密码存储在那里,并让你的当Selenium代码执行时,Selenium客户端代码从那里检索用户名和密码。

答案 1 :(得分:5)

这是因为selenium服务器每次都会为浏览器启动新的配置文件,因此此配置文件中不存在您保存的Cookie和书签。

首先创建一个配置文件,对于firefox,它被赋予here

然后将此配置文件捆绑到您的selenium服务器,如下所示

    SeleniumServer server = new SeleniumServer();
    RemoteControlConfiguration rcc = new RemoteControlConfiguration();
    //rcc.setPort(4444);
    File newFirefoxProfileTemplate = new File(ReadConFile.readcoFile("fiefoxProfilePath"));

    rcc.setFirefoxProfileTemplate(newFirefoxProfileTemplate);
    server = new SeleniumServer(rcc);
    server.start();
    DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*chrome",ReadConFile.readcoFile("serverName"));

要了解您的firefoxTemplate,请点击here

相关问题