亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Python 在 iframe 中切換 Selenium 焦點

如何使用 Python 在 iframe 中切換 Selenium 焦點

暮色呼如 2023-02-22 16:27:16
我無法從網站上的下拉菜單中選擇復選框。下拉菜單位于 iframe 內,當我嘗試切換到 iframe 時,我不斷收到TimeoutException消息:。下面是我正在嘗試的代碼和 HTML。我是 Python 的新手,所以對此的任何幫助將不勝感激。Python:wait = WebDriverWait(driver, 20)frm1= wait.until(EC.presence_of_element_located((By.ID, "ReportViewerControl_ctl04_ctl03_ctl01")))driver.switch_to.frame(frm1)HTML:<iframe id="ReportViewerControl_ctl04_ctl03_ctl01" onclick="event.cancelBubble=true;" onactivate="event.cancelBubble=true;" style="display: block; position: absolute; z-index: 10; left: 208px; top: 35px; width: 198px; height: 166px;" src="javascript:'';" frameborder="0" title="Area place holder" longdesc="Area place holder" name="ReportViewerControl_ctl04_ctl03_ctl01"></iframe>
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

要在而不是中切換Selenium的焦點,您必須:<iframe>presence_of_element_located()

  • 誘導WebDriverWait以等待所需的框架可用并切換到它

  • 您可以使用以下任一定位器策略

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='ReportViewerControl'][longdesc='Area place holder']")))
    • 使用XPATH

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id, 'ReportViewerControl') and @title='Area place holder']")))
    • 注意:您必須添加以下導入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC


查看完整回答
反對 回復 2023-02-22
  • 1 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號