Selenium / Python切换webdriver从无头到窗口模式

时间:2018-03-06 15:29:05

标签: python selenium selenium-webdriver

有没有办法将Chrome webdriver从无头模式切换到窗口模式?

我想到的一件事就是切换'现有的Web驱动程序到非无头模式。 另一个想法:用某种状态创建新的webdriver实例(这次是无头的)。从旧的,所以用户操作可以执行。我不知道该怎么做或者是否有可能。

import os
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException,

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(
    executable_path=os.path.join(os.getcwd(), 'chromedriver'),
    chrome_options=options,
    )
driver.get('https://website.com')
try:
    driver.find_element_by_xpath('//h1').click()
except NoSuchElementException:
    print('You have to click it manually')
    # here I need Chrome browser
    # to be opened so that I can click a link
print('The name of this thing is: ', end='')
print(driver.find_element_by_xpath("//h1[@class='name']").text)

1 个答案:

答案 0 :(得分:0)

如果您需要打开新标签页

DECLARE @CurrentDate as date = GETDATE()

INSERT INTO [TBL_ParseRawDataHist]  
SELECT   [SrcID],[ASOFDATE],
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 1) AS Parse1, 
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 2) AS Parse2,
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 3) AS Parse3, 
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 4) AS Parse4, 
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 5) AS Parse5, 
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 6) AS Parse6, 
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 7) AS Parse7, 
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 8) AS Parse8,
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 9) AS Parse9, 
         dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 10) AS Parse10          
FROM     TBL_FR2052A_RAW_DATA_HIST  
WHERE    ASOFDATE = DATEADD(DAY, -DAY(@CurrentDate), @CurrentDate)

如果您需要切换到新的

driver.execute_script("window.open()")

然后你得到页面

driver.switch_to.window(self.driver.window_handles[1])

结束你可以关闭它(新的)

driver.get('https://website.com')

然后回到第一个驱动程序

driver.close()