WebDriver' object has no attribute 'switch_to_frame'

Han*_*lin 6 python iframe selenium frame selenium-webdriver

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'
Run Code Online (Sandbox Code Playgroud)

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-------------------");

...
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 5

切换到frame时,支持的符号是:


这个用例

要切换您使用的框架:

driver.switch_to.frame(frame2.tag_name);
Run Code Online (Sandbox Code Playgroud)

也就是说,TAG_NAME不受支持。因此你会看到错误:

Message='WebDriver' object has no attribute 'switch_to_frame'
Run Code Online (Sandbox Code Playgroud)

解决方案

您可以使用以下代码行:

# 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);
Run Code Online (Sandbox Code Playgroud)

  • 这不是一个拼写错误,但我猜这个语法可能已从旧驱动程序版本中被弃用,现在仅支持 driver.switch_to.frame(#) 。我们有一个应用程序,它的语法与您的问题相同,并且开始失败/错误(不确定从什么时候开始,但是当我开始调试失败时),这个答案帮助解决修复它。 (2认同)