Javascript 在Selenium中选择弹出窗口

Javascript 在Selenium中选择弹出窗口,javascript,python,selenium,google-chrome,google-chrome-extension,Javascript,Python,Selenium,Google Chrome,Google Chrome Extension,我试图在Selenium脚本中使用以下chrome扩展: 它将用于记录与浏览器的一些交互 下面的代码打开Selenium,获取目标URL(在本例中为example.com),加载扩展,并退出在加载扩展时打开的弹出窗口 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from seleniu

我试图在Selenium脚本中使用以下chrome扩展: 它将用于记录与浏览器的一些交互

下面的代码打开Selenium,获取目标URL(在本例中为example.com),加载扩展,并退出在加载扩展时打开的弹出窗口

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

class NumWindows():
    """
    Used by WebDriverWait.until() method
    """
    def __init__(self, num_windows):
        self.num_windows = num_windows

    def __call__(self, driver):
        while len(driver.window_handles) < self.num_windows:
            return False
        return True

opts = Options()

ext_dir = ('/chrome-extensions/')
screenshot_and_screen_video_recorder = os.path.join(ext_dir, 'jgmmgiojkjopgnanopiamhbhnpaednfg/1.0.2_0')
# ext_dir takes the ext dir as a key and the pop-up window that opens on start-up
# as a value. Later this pop-up windows is closed out.
ext_dir = {
    screenshot_and_screen_video_recorder: 'https://screeny-app.com/thankyou/'
}
opts.add_argument(f"--load-extension={screenshot_and_screen_video_recorder}")
executable_path = os.path.join(parent_dir, 'chromedriver')

# initiate the driver
driver = webdriver.Chrome(executable_path=executable_path, options=opts)
driver.get('example.com')

# Wait for ext widnows to open, then close them out and switch back to the
# target url
WebDriverWait(driver, 30).until(NumWindows(len(ext_dir)+1))
for tab in driver.window_handles:
    driver.switch_to.window(tab)
    if driver.current_url in ext_dir.values():
        driver.close() # Only closes current tab, not browser
# Switch to original tab
driver.switch_to.window(driver.window_handles[0])
这将弹出此弹出窗口。

下一步可以单击录制选项卡,这会将我们带到弹出窗口中的一个新窗口。下面是新图片

record_tab = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.ID, "record-tab"))
)
record_tab.click()

单击窗口的最后一个按钮有点棘手,但使用下面的代码是可行的。这就引出了 一个新的弹出窗口

sm_record_desktop = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.ID, "sm_record_desktop"))
)
# with sm_record_desktop.click() the following Exception occurs
# selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
# Clicking this button closes the screen, and results in one window again.
driver.execute_script("arguments[0].click();", sm_record_desktop)
driver.switch_to.window(driver.window_handles[0])

深入研究SeleniumWebDriver源代码,有几种方法可以改变焦点。我无法使用以下任何方法处理此弹出窗口

#python code
    @property
    def switch_to(self):
        """
        :Returns:
            - SwitchTo: an object containing all options to switch focus into
    
        :Usage:
            element = driver.switch_to.active_element
            alert = driver.switch_to.alert
            driver.switch_to.default_content()
            driver.switch_to.frame('frame_name')
            driver.switch_to.frame(1)
            driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
            driver.switch_to.parent_frame()
            driver.switch_to.window('main')
        """
        return self._switch_to
要充分使用扩展,我需要能够单击Chrome选项卡,从列表中选择一个选项卡并单击共享按钮。

我无法使用浏览器工具检查此弹出窗口,也无法让Selenium将焦点切换到它。 如果有人有任何经验与这些类型的浏览器扩展之前,我想知道一个解决方案的问题

#python code
    @property
    def switch_to(self):
        """
        :Returns:
            - SwitchTo: an object containing all options to switch focus into
    
        :Usage:
            element = driver.switch_to.active_element
            alert = driver.switch_to.alert
            driver.switch_to.default_content()
            driver.switch_to.frame('frame_name')
            driver.switch_to.frame(1)
            driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
            driver.switch_to.parent_frame()
            driver.switch_to.window('main')
        """
        return self._switch_to