1

我无法从网站上的下拉菜单中选择复选框。下拉列表位于 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>
4

1 回答 1

0

要在而不是切换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
      

参考

您可以在以下位置找到一些相关的讨论:

于 2020-07-06T17:25:31.513 回答