WebDriver' object has no attribute 'switch_to_frame'
Asked Answered
J

2

6

I cannot switch to the sucessfully identified iFrame(s). The script identifies the iFrame (checked in debugger), but the switch to the iFrame fails and runs into the exception trap. Few times ago it worked perfectly.

Message='WebDriver' object has no attribute 'switch_to_frame'

What happened in the meantime?

Chromedriver has been updated from version 95.0.4638.17 to ChromeDriver 96.0.4664.45

Is the Chromedriver is no longer compatible with the latest Selenium version?

... 
driver.switch_to.default_content()

try:
    # find the frame
    wait.until(EC.element_to_be_clickable((By.ID, "wysiwygTextarea_ifr")))
    frame2 = driver.find_element(By.XPATH, "//iframe[@id='wysiwygTextarea_ifr']");
    
    # switch to frame
    driver.switch_to.frame(frame2.tag_name);

    print("--------------iframe found-------------------"); 
except:
    print("--------------iframe not found-------------------");

...
Judyjudye answered 2/12, 2021 at 10:39 Comment(3)
Once more, as I do remember also Selenium did sponsor an update.Judyjudye
Once more I can add here. Today I could configure out why it was working as suggested by DebanjanB and next time it didn't work anymore. As I downgraded by development environment from Selenium 4.x to the 3.x all scripts are now running perfectly.Judyjudye
Or update on Selenium only via pip install -U selenium and do NOT install a new Selenium on top, this implies a Selenium Mix. PROBLEM SOLVED!!!Judyjudye
F
5

While switching to frame, the supported notations are:

  • Switch to a frame using frame name:

    driver.switch_to.frame('frame_name')
    
  • Switch to a frame using frame index:

    driver.switch_to.frame(1)
    
  • Switch to a frame using frame element:

    driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
    
    • Switch to parent frame:

      driver.switch_to.parent_frame()

  • Switch to default content:

    driver.switch_to.default_content()
    

This usecase

To switch the frame you have used:

driver.switch_to.frame(frame2.tag_name);

that is, the TAG_NAME which isn't supported. Hence you see the error:

Message='WebDriver' object has no attribute 'switch_to_frame'

Solution

You can use the following line of code:

# find the frame
wait.until(EC.element_to_be_clickable((By.ID, "wysiwygTextarea_ifr")))
frame2 = driver.find_element(By.XPATH, "//iframe[@id='wysiwygTextarea_ifr']");
    
# switch to frame by frame element
driver.switch_to.frame(frame2);
Fdic answered 2/12, 2021 at 11:1 Comment(2)
Well, this fixed my problem, although I also used just "frame2" earlier (probably a typo went un undiscovered). Anyway, thanks you mate for your support in this issue.Judyjudye
It wasn't a typo, but I guess this syntax may have been deprecated from old driver version and now only driver.switch_to.frame(#) is only supported. we had an app that had the same syntax as your question and that started to fail/error out (not sure since when, but when i started to debug for the failures) and this answer help solve fixed it.Snooty
F
0

driver._switch_to.frame only works

Farthingale answered 27/8, 2022 at 20:9 Comment(1)
_switch_to or switch_to? I think you meant the former. But, if so, isn't that well-covered in @undetected Selenium's answer from last year?Surovy

© 2022 - 2024 — McMap. All rights reserved.