webdriver 解决shadow-root获取chrome下载项

jjddrushi 2020-05-09

def _return_element(self, ele):
        return self.driver.driver.execute_script("return arguments[0].shadowRoot", ele)
    
    def verify_dl(self):
        """
        返回最近一个下载,目前仅支持chrome
        :return:
        """
        self.driver.execute_script(‘window.open("")‘)
        win_lst = self.driver.window_handles
        self.driver.switch_to_window(win_lst[-1])
        self.driver.get(‘chrome://downloads‘)
        first_sd = self._return_element(self.driver.find_element_by_css_selector(‘css=downloads-manager‘))
        second_sd = self._return_element(first_sd.find_element_by_css_selector("#downloadsList>downloads-item"))
        name, dl_url = second_sd.find_element_by_css_selector(‘#details‘).text.split(‘\n‘)[:2]
        self.driver.close()
        self.driver.switch_to_window(win_lst[0])
        return {‘name‘: name, ‘dl_url‘: dl_url}

相关推荐