diff --git a/nodriver/core/browser.py b/nodriver/core/browser.py index 05f1708..340bd05 100644 --- a/nodriver/core/browser.py +++ b/nodriver/core/browser.py @@ -638,6 +638,8 @@ async def get_all( continue connection = tab break + else: + connection = self._browser.connection cookies = await connection.send(cdp.storage.get_cookies()) if requests_cookie_format: import requests.cookies @@ -670,6 +672,8 @@ async def set_all(self, cookies: List[cdp.network.CookieParam]): continue connection = tab break + else: + connection = self._browser.connection cookies = await connection.send(cdp.storage.get_cookies()) await connection.send(cdp.storage.set_cookies(cookies)) @@ -701,6 +705,8 @@ async def save(self, file: PathLike = ".session.dat", pattern: str = ".*"): continue connection = tab break + else: + connection = self._browser.connection cookies = await connection.send(cdp.storage.get_cookies()) # if not connection: # return @@ -752,6 +758,8 @@ async def load(self, file: PathLike = ".session.dat", pattern: str = ".*"): continue connection = tab break + else: + connection = self._browser.connection cookies = await connection.send(cdp.storage.get_cookies()) for cookie in cookies: for match in pattern.finditer(str(cookie.__dict__)): @@ -780,6 +788,8 @@ async def clear(self): continue connection = tab break + else: + connection = self._browser.connection cookies = await connection.send(cdp.storage.get_cookies()) await connection.send(cdp.storage.clear_cookies()) diff --git a/nodriver/core/tab.py b/nodriver/core/tab.py index 9b61a76..2e2a15b 100644 --- a/nodriver/core/tab.py +++ b/nodriver/core/tab.py @@ -355,11 +355,11 @@ async def query_selector_all( if _node.node_name == "IFRAME": doc = _node.content_document node_ids = [] + try: node_ids = await self.send( cdp.dom.query_selector_all(doc.node_id, selector) ) - # await self.send(cdp.dom.disable()) except ProtocolException as e: if _node is not None: @@ -375,6 +375,7 @@ async def query_selector_all( ) return await self.query_selector_all(selector, _node) else: + await self.send(cdp.dom.disable()) raise if not node_ids: return [] @@ -412,9 +413,10 @@ async def query_selector( if _node.node_name == "IFRAME": doc = _node.content_document node_id = None + try: node_id = await self.send(cdp.dom.query_selector(doc.node_id, selector)) - # await self.send(cdp.dom.disable()) + except ProtocolException as e: if _node is not None: if "could not find node" in e.message.lower(): @@ -429,6 +431,7 @@ async def query_selector( ) return await self.query_selector(selector, _node) else: + await self.send(cdp.dom.disable()) raise if not node_id: return @@ -463,7 +466,7 @@ async def find_elements_by_text( node_ids = [] await self.send(cdp.dom.discard_search_results(search_id)) - await self.send(cdp.dom.disable()) + results = [] for nid in node_ids: node = util.filter_recurse(doc, lambda n: n.node_id == nid) @@ -509,6 +512,7 @@ async def find_elements_by_text( iframe_text_elems = [element.create(text_node, self, iframe_elem.tree) for text_node in iframe_text_nodes] results.extend(text_node.parent for text_node in iframe_text_elems) + await self.send(cdp.dom.disable()) return results or [] async def find_element_by_text( @@ -539,7 +543,7 @@ async def find_element_by_text( # return node_ids = await self.send(cdp.dom.get_search_results(search_id, 0, nresult)) await self.send(cdp.dom.discard_search_results(search_id)) - await self.send(cdp.dom.disable()) + if not node_ids: node_ids = [] results = [] @@ -580,20 +584,23 @@ async def find_element_by_text( if iframe_text_nodes: iframe_text_elems = [element.create(text_node, self, iframe_elem.tree) for text_node in iframe_text_nodes] results.extend(text_node.parent for text_node in iframe_text_elems) - if not results: - return - if best_match: - closest_by_length = min( - results, key=lambda el: abs(len(text) - len(el.text_all)) - ) - elem = closest_by_length or results[0] + try: + if not results: + return + if best_match: + closest_by_length = min( + results, key=lambda el: abs(len(text) - len(el.text_all)) + ) + elem = closest_by_length or results[0] - return elem - else: - # naively just return the first result - for elem in results: - if elem: - return elem + return elem + else: + # naively just return the first result + for elem in results: + if elem: + return elem + finally: + await self.send(cdp.dom.disable()) async def back(self): """ diff --git a/nodriver/core/util.py b/nodriver/core/util.py index e279d72..255d87b 100644 --- a/nodriver/core/util.py +++ b/nodriver/core/util.py @@ -138,7 +138,7 @@ def deconstruct_browser(): break time.sleep(0.15) continue - print("successfully removed temp profile %s", _.config.user_data_dir) + print("successfully removed temp profile %s" % _.config.user_data_dir) def filter_recurse_all(