如何在selenium python中打开新的隐身窗口

时间:2017-01-03 11:56:51

标签: selenium-webdriver

如何打开新的隐身窗口?我的意思是当迭代完成后我想打开新的隐身窗口:

a.userlabel {
    display:inline-block !important;
    text-decoration: underline !important;
    background-color:coral !important;
}

但是当我写这样的代码时:

class JoinPage(unittest.TestCase):

@classmethod
def setUpClass(cls):

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--incognito')
    chrome_options.add_argument('--start-maximized')
    cls.browser = webdriver.Chrome('/home/andrew/Downloads/chromedriver', chrome_options=chrome_options)

def test_01_new_account_jp(self):
    with open('/home/andrew/PycharmProjects/test/jpage/accounts.csv', 'rb', ) as csvfile:
        the_file = csv.reader(csvfile, delimiter=',')
        for row in the_file:
            for i in range(2):
                self.browser.get('http://localhost:5000')
                user = row[0]
                password = row[1]
                self.browser.xpath('html/body/div[1]/div/div/div[2]/form/div[2]/div/button').click()
                self.browser.xpath(".//*[@id='Email']").send_keys(user)
                self.browser.xpath(".//*[@id='next']").click()
                self.browser.xpath(".//*[@id='Passwd']").send_keys(password)

工作。我知道这样写是不对的。

1 个答案:

答案 0 :(得分:0)

您可以针对常用浏览器尝试此代码。希望这会有所帮助。

  • 铬:

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("incognito");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    
  • 的FireFox:

    FirefoxProfile firefoxProfile = new FirefoxProfile();    
    firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
    
  • Internet Explorer:

    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
    capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true); 
    capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
    
  • 歌剧:

    DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();
    OperaOptions options = new OperaOptions();
    options.addArguments("private");
    capabilities.setCapability(OperaOptions.CAPABILITY, options);