“Malformed URL: can't access dead object” in s

2020-05-07 03:48发布

With python selenium I am currently developing a very complex test case. In the midst of it (page might be loading, with iframes inside iframes etc), I just want to open a webpage by doing as follows:

driver.get("https://www.google.com")

but I get the following error:

InvalidArgumentException: Message: Malformed URL: can't access dead object

Can't I open just a webpage somewhere in my selenium tests? Why is this a malformed URL? What do dead objects have to do with navigating to a new webpage...?

2条回答
可以哭但决不认输i
2楼-- · 2020-05-07 04:30

I think you switched into a frame just before .get(). And you cannot open an url in the frame. Try driver.switch_to_default_content()

查看更多
贪生不怕死
3楼-- · 2020-05-07 04:33

Why do we see the error can't access dead object

Lets us assume we have a Web Page which contains an iframe which is inside another iframe. So the hierarchy is as follows :

page root (grandparent) -> iframe (parent) -> iframe (child)

Now, within the iframe (child) if we interect with an WebElement (example click a button) that destroys/closes the child, we may face the error can't access dead object.

Reason :

It seems that the driver is trying to maintain a reference to the button that was clicked. However, the button no longer exists and thus the webdriver implodes.

What could have went wrong :

In an attempt to improve memory usage and to prevent memory leaks Firefox disallows add-ons to keep strong references to DOM objects after their parent document has been destroyed. A dead object holding a strong reference to a DOM element that persists even after it was destroyed in the DOM is the root cause of this error.

As an example, the current implementation of GeckoDriver do not always check for a valid top-level browsing context. Which means in case of navigating away from a frame and trying to invoke get() method, which might close it, the following command may fail because the context is no longer available.

Solution :

In these cases, the solution is to navigate back to the default_content or parent_frame and then try to invoke get() method as follows :

driver.switch_to.frame("iframe_name")
//frame tasks
driver.switch_to.default_content()
driver.get("https://www.google.com")

Here are the references of this Answer.

查看更多
登录 后发表回答