python - Selenium 与Python : First instance of the element is identified but the next occurance is ElementNotVisibleException

标签 python django selenium

我有以下针对 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')

从打印结果中可以清楚地识别出电子邮件箱的第一次出现,并断言占位符等于。 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 -->

请帮忙。

最佳答案

实际上 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)

关于python - Selenium 与Python : First instance of the element is identified but the next occurance is ElementNotVisibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39152629/

相关文章:

python - 按值对 python 中的字典进行排序,但保持相等值的键顺序

python - 如何在 Python 中获取 XML 标签值

python - tensorflow 条件: check if the values inside the tensor is zero or greater

python - Django - 找出我在内联表单集中编辑的模型实例

python - 如何在python shell中导入virtualenv中的包

java - 尝试在 webDriver 中使用 @FindBy 时出现 java.lang.NullPointerException

java - 如何使用 Apache POI 和 Java 创建超链接?

python - 访问属性时,发送到另一个实例

django - 将 celery 作为守护进程运行不会创建 PID 文件

python - 在 Python 中使用带有 Selenium 的默认 Chrome 配置文件