Selenium WebDriverWait 不断抛出 TimeoutException

Selenium WebDriverWait keeps throwing a TimeoutException

我使用 Selenium 登录如下:

driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,'email'))).send_keys("myemail@gmail.com")

但我一直收到错误消息:

TimeoutException: Message:

我也试过:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='email']"))).send_keys("myemail@gmail.com")

这是我正在访问的 HTML 源代码:

<div id="signup-form">
    <div class="logo"></div>
    <h1 data-i18n="signin">Sign in</h1>
    <div class="panels-wrapper">
        <div class="panels">
            <div class="panel step-1">
                <input name="email" type="text" id="email" required="" maxlength="100">
                <label for="email" data-i18n="email">Email</label>

感谢解决此问题的任何指导。

电子邮件不是框架。您使用的条件用于框架( iframe 标签),但在这里您处理的是输入标签。因此,我建议您根据您的要求检查该元素的可见性或该元素的存在性。

您需要注意几件事。

如果该元素在您共享的 / , without the relevant it would be tough to construct a canonical answer. However, as per the HTML 中,我看不到任何 <frame> / <iframe>.

的存在

通常,要在您需要使用 for the element_to_be_clickable() and you can use either of the following :

的元素中发送 字符序列
  • CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#email[name='email']"))).send_keys("myemail@gmail.com")
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='email' and @name='email']"))).send_keys("myemail@gmail.com")
    

You can find a relevant discussion in


如果元素在 a 内,您必须:

  • 诱导 WebDriverWait 以获得所需的 框架并切换到它.
  • 诱导 WebDriverWait 使所需的 元素可点击
  • 您可以使用以下任一项:

    • 使用CSS_SELECTOR:

      driver.get('site_url')
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe_cssSelector")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#email[name='email']"))).send_keys("myemail@gmail.com")
      
    • 使用XPATH:

      driver.get('site_url')
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"iframe_xpath")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='email' and @name='email']"))).send_keys("myemail@gmail.com")
      
    • 注意:您必须添加以下导入:

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

      You can find a relevant discussion in


参考

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

  • Switch to an iframe through Selenium and python