我無法切換到成功識別的 iFrame。腳本識別 iFrame(在除錯器中檢查),但切換到 iFrame 失敗并運行到例外陷阱。前幾次它作業得很好。
Message='WebDriver' object has no attribute 'switch_to_frame'
這期間發生了什么?
Chromedriver 已從版本 95.0.4638.17 更新到 ChromeDriver 96.0.4664.45
Chromedriver 是否不再與最新的 Selenium 版本兼容?
... 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-------------------"); ...
uj5u.com熱心網友回復:
切換到frame 時,支持的符號是:
使用框架名稱切換到框架:
driver.switch_to.frame('frame_name')使用幀索引切換到幀:
driver.switch_to.frame(1)使用frame element切換到框架:
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])-
切換到父框架:
driver.switch_to.parent_frame()
切換到默認內容:
driver.switch_to.default_content()
這個用例
要切換您使用的幀:
driver.switch_to.frame(frame2.tag_name);
也就是說,TAG_NAME哪個不受支持。因此,您會看到錯誤:
Message='WebDriver' object has no attribute 'switch_to_frame'
解決方案
您可以使用以下代碼行:
# 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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/376412.html
