【问题标题】:Selenium with Python: First instance of the element is identified but the next occurance is ElementNotVisibleExceptionSelenium 与 Python:元素的第一个实例被识别,但下一个实例是 ElementNotVisibleException
【发布时间】:2016-08-25 18:44:32
【问题描述】:

我有以下适用于 Python/Django 应用程序的 Selenium 测试:

class EmailRecordsTest(StaticLiveServerTestCase):

    def test_can_store_email_and_retrieve_it_later(self):
        self.browser.get(self.live_server_url)
        emailbox = self.browser.find_element_by_xpath("//form[@class='pma-subscribe-form']/input[1]")
        self.assertEqual(emailbox.get_attribute("placeholder"), 'Enter your Email')
        print("tested until here")
        print("The placeholder: ", emailbox.get_attribute("placeholder"))
        print(emailbox)
        emailbox.send_keys('vio@mesmerizing.com')

从打印运行中可以清楚地识别出第一次出现的邮箱,并为占位符断言 Equal。 emailbox.send_keys 的最后一个实例抛出以下错误:

 selenium.common.exceptions.ElementNotVisibleException: Message:
 Element is not currently visible and so may not be interacted with

找不到与 send_keys 一起使用时相同元素变为不可见的原因。

正在测试的Html代码如下:

<!-- Start footer -->
  <footer id="pma-footer">
    <!-- start footer top -->
    <div class="pma-footer-top">
      <div class="container">
        <div class="pma-footer-top-area">
          <div class="row">
            <div class="col-lg-3 col-md-3 col-sm-3">
              <div class="pma-footer-widget">
                <h4>News letter</h4>
                <p>Get latest update, news & offers</p>
                <form class="pma-subscribe-form">
                  <input id="subscribe-email" type="email" placeholder="Enter your Email">
                  <button class="btn btn-danger btn-md" type="submit">Subscribe!</button>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- end footer top -->

请帮忙。

【问题讨论】:

    标签: python django selenium


    【解决方案1】:

    实际上find_element 返回的元素将出现在DOM 上,无论它是否可见,您也可以获得该元素的属性,但send_keys 对元素执行操作,而 selenium 仅对可见元素执行操作, 因此在对元素执行操作之前,您需要确保使用 WebDriverWait 可以看到它,如下所示:-

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    
    emailbox = wait.until(EC.visibility_of_element_located((By.ID, "subscribe-email")))
    
    #do your all stuff before send keys 
    
    # now use send_keys
    emailbox.send_keys('vio@mesmerizing.com')
    

    已编辑:- 如果您仍然无法与元素交互,请尝试使用execute_script() 设置值如下:-

    emailbox = wait.until(EC.presence_of_element_located((By.ID, "subscribe-email")))
    
    #do your all stuff before send keys 
    
    # now use execute_script
    driver.execute_script("arguments[0].value = 'vio@mesmerizing.com'", emailbox)
    

    【讨论】:

    • 这不起作用,即使在等待时间为 60 之后也是如此。它总是超时。 selenium.common.exceptions.TimeoutException: Message:
    • @Blueice 好的,然后尝试使用presence_of_element_located 查找元素并使用execute_script() 设置值,尝试编辑答案并让我知道..:)
    • execute_script 解决方案有效。我还观察到的是 execute_script 工作,我认为我们不需要 WebDriverWaitemailbox = wait.until(EC.presence_of_element_located((By.ID, "subscribe-email"))) 。该语句能够通过 xpath 找到 emailbox WebElement。
    • @Blueice 是的,但这是定位元素的最佳方式,但如果您不需要它,您可以简单地使用driver.find_element_by_id(..)instead。:)
    • @Blueice 如果您不介意,可以将其标记为正确答案,如果有帮助的话..:)
    【解决方案2】:

    在这种情况下有效的另一个选项是滚动到特定元素(位于页面底部),然后使用 send_keys 它可以工作。

    emailbox = self.browser.find_element_by_xpath("//form[@class='mu-subscribe-form']/input[1]")
    self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    emailbox.send_keys('vio@mesmerizing.com')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多