2

In a rather complex python-selenium test script (which is not accessible from the outside, so I cannot provide an example), I have the following line waiting for an element to become visible:

WebDriverWait(basedriver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"clb-iframe-workspace")))
elem = WebDriverWait(basedriver, 20).\
           until(EC.element_to_be_clickable((By.XPATH, '//div[contains(text(), "Feature extraction")]')))

In most cases there is no problem and the web driver finds the element in question. But sometimes (about 1 in 10 cases) I get the following error:

WebDriverException: Message: TypeError: can't access dead object

And strangely that happens right away! Selenium does not even bother to wait the 20 seconds I want selenium to wait by using WebDriverWait!

Maybe the iframe switch could have something to do with it? I also tried to make a screenshot just before the failing line - I get a blank white screen in both cases. So how to debug this any further myself? And why does the WebDriverWait not wait...?

Addendum

  • I have implemented the suggestion from DebanjanB, but I still get the same error at a level of 5%.
  • If I use a slower internet connection, the error takes place almost all the time!
  • When I insert code to take screenshots and write out the page source (which takes some time to do), the error rate decreases again.

So I would say its definitive a timing issue, combined with a bug in selenium...

Alex
  • 41,580
  • 88
  • 260
  • 469
  • Why not google this error? Someone says adding `switch_to_default_content()` before will work. – Sraw Jul 05 '18 at 09:42
  • I need to switch to an iframe just before I look for this element, so switching back to the default content is completely wrong! – Alex Jul 05 '18 at 09:45
  • Claim down. There are many different situations. For example, you forget to switch back from an iframe. Or the iframe is reloaded before you access the elements. Basically, you are not in the right content. – Sraw Jul 05 '18 at 09:52
  • But why does it work 90% of the times, and sometimes it does not work? – Alex Jul 05 '18 at 09:53
  • 1
    https://stackoverflow.com/questions/47811895/how-to-properly-wait-for-a-frame-to-be-available-in-python-selenium – Alex Jul 05 '18 at 13:20

1 Answers1

1

As per best practices switching to an <iframe> must be implemented inducing WebDriverwait with expected_conditions clause frame_to_be_available_and_switch_to_it(locator).


class frame_to_be_available_and_switch_to_it(object)

class frame_to_be_available_and_switch_to_it(object) is the expectation for checking whether the given frame is available to switch to. If the frame is available it switches the given driver to the specified frame.


So you need to replace the lines:

elem = WebDriverWait(basedriver, 20).\
        until(EC.presence_of_element_located((By.ID, 'clb-iframe-workspace')))
basedriver.switch_to_frame(elem)

as:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"clb-iframe-workspace")))

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for that suggestion I will try it. However, where can I find such 'best practices'? Or can you suggest a dicument/book which explains all the inner details of selenium and the webdriver in greatest technical details, so I can learn and understand all about the htlm dom, frames, driver, geckodriver etc etc? – Alex Jul 05 '18 at 10:06
  • The error did reappear. Your suggestion did not help to fix my problem. But I still would be greatful if you can provide a really good in-depth and detailed technical documentation (which, for examples, explains EACH possible selenium/webdriver error in great detail. ...) – Alex Jul 05 '18 at 10:11
  • Ok I solved the problem. The frame changes inbetween. A new frame is being loaded. I have to check the title... – Alex Jul 06 '18 at 07:49
  • @Alex As you asked, I am in the process of preparing a detailed technical documentation on frame handling – undetected Selenium Jul 06 '18 at 08:19