Selenium with Python: First instance of the element is identified but the next occurance is ElementNotVisibleException
<p>I have the following Selenium Test for Python/Django application:</p>

<pre><code>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')
</code></pre>

<p>First occurance of emailbox is clearly identified as seen from the print runs and assert Equal for placeholder. The last instance of emailbox.send_keys throws following error:</p>

<pre><code> selenium.common.exceptions.ElementNotVisibleException: Message:
 Element is not currently visible and so may not be interacted with
</code></pre>

<p>Cannot find why the same element become Not Visible when using with send_keys.</p>

<p>The Html code being tested is as below:</p>

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

<p>Kindly help.</p>

<p>Actually <code>find_element</code> returns element which would be present on the <code>DOM</code> no matter it's visible or not and you can get attribute of this element as well but <code>send_keys</code> does an action on element and selenium does action only visible element, So you need to be sure before doing action on element that it's visible using <code>WebDriverWait</code> as below :-</p>

<pre><code>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')
</code></pre>

<p><strong>Edited</strong> :- If you are still unable to interact with element try using <code>execute_script()</code> to set value as below :-</p>

<pre><code>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)
</code></pre>

python django selenium