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?
Run Code Online (Sandbox Code Playgroud)... 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-------------------"); ...
切换到frame时,支持的符号是:
使用框架名称切换到框架:
driver.switch_to.frame('frame_name')
Run Code Online (Sandbox Code Playgroud)
使用帧索引切换到帧:
driver.switch_to.frame(1)
Run Code Online (Sandbox Code Playgroud)
使用框架元素切换到框架:
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
Run Code Online (Sandbox Code Playgroud)
切换到父框架:
driver.switch_to.parent_frame()
切换到默认内容:
driver.switch_to.default_content()
Run Code Online (Sandbox Code Playgroud)
要切换您使用的框架:
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)