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

Him*_*shu 5 python selenium

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

Phi*_*ger 8

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

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

但是,不要在代码中硬编码用户名和密码,而是在客户端本地使用用户名和密码保留一个故意非源代码修订的配置文件,并在其中存储用户名和密码,并拥有Selenium客户端代码当Selenium代码执行时,从那里检索用户名和密码.


9ik*_*han 5

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

首先创建一个配置文件,对于firefox它在这里给出

然后将此配置文件捆绑到您的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"));
Run Code Online (Sandbox Code Playgroud)

要了解你的firefoxTemplate,请点击这里