Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium with Python:元素的第一个实例已标识,但下一个实例是ElementNotVisibleException_Python_Django_Selenium - Fatal编程技术网

Selenium with Python:元素的第一个实例已标识,但下一个实例是ElementNotVisibleException

Selenium with Python:元素的第一个实例已标识,但下一个实例是ElementNotVisibleException,python,django,selenium,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("//f

我对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的首次出现,并为占位符断言相等。emailbox.send\u keys的最后一个实例引发以下错误:

 selenium.common.exceptions.ElementNotVisibleException: Message:
 Element is not currently visible and so may not be interacted with
无法找到同一元素在与send_键一起使用时不可见的原因

正在测试的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_元素
返回将出现在
DOM
上的元素,不管它是否可见,您也可以获取该元素的属性,但是
send_键
对元素执行操作,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')
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)
已编辑:-如果您仍然无法与元素交互,请尝试使用
execute_script()
设置如下值:-

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')
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)

在本例中,另一个有效的选项是滚动到特定元素(位于页面底部),然后使用send_键

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')

这不起作用,即使在等待60分钟之后。它总是超时
selenium.common.exceptions.TimeoutException:Message:
@Blueice Ok然后尝试使用
presence\u of_element\u查找元素,并使用
execute\u script()
设置值,尝试编辑答案并让我知道……)execute\u脚本解决方案有效。我还观察到,
execute\u script
要工作,我认为我们不需要
WebDriverWait
emailbox=wait.until(EC.presence\u of_element\u located((By.ID,“subscribe email”))
。该语句可以通过xpath查找找到emailbox WebElement。@Blueice是的,但这是查找元素的最佳方法,但如果您不需要它,可以简单地使用
驱动程序。改为通过id(…)
查找元素。:@Blueice如果您不介意,可以将其标记为正确答案,如果有帮助的话:)