1

Currently working to use Python to login with Twitter.

Twitter's login page is here. The source code where the Username and Password input fields are:

<div class="LoginForm-input LoginForm-username">
<input
  type="text"
  class="text-input email-input js-signin-email"
  name="session[username_or_email]"
  autocomplete="username"
  placeholder="Phone, email or username"
/>
</div>

<div class="LoginForm-input LoginForm-password">
<input type="password" class="text-input" name="session[password]" 
placeholder="Password" autocomplete="current-password">
</div>

So when I write my code in Python utilizing the Selenium module:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://twitter.com/login")
elem = driver.find_element_by_name("session[username_or_email]")
elem.clear()
elem.send_keys(username)
elem = driver.find_element_by_name("session[password]")
elem.clear()
elem.send_keys(password)
elem.send_keys(Keys.RETURN)
sleep(delay)

The error that is returned:

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

Any help? Thanks! I have read of the responses of other similar questions, but have not helped much.

Edit:

Full error message:

Traceback (most recent call last):
  File "main.py", line 154, in <module>
    main()
  File "main.py", line 143, in main
    twitterBruteforce(username, wordlist, delay)
  File "src/twitterLib.py", line 27, in twitterBruteforce
    elem.clear()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 88, in clear
    self._execute(Command.CLEAR_ELEMENT)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
    at fxdriver.preconditions.visible (file:///tmp/tmpurkUhr/extensions/[email protected]/components/command-processor.js:10092)
    at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpurkUhr/extensions/[email protected]/components/command-processor.js:12644)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpurkUhr/extensions/[email protected]/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpurkUhr/extensions/[email protected]/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpurkUhr/extensions/[email protected]/components/command-processor.js:12608)
8
  • which exact element is not visible? show full exception log Commented Sep 4, 2016 at 18:51
  • updated my post. thanks :) Commented Sep 5, 2016 at 0:00
  • Not clear.. could you tell us in which line this exception occurred?? Is this exception occurred at elem.clear() line?? But you are using same variable for both element why?? Commented Sep 5, 2016 at 1:32
  • 'elem = driver.find_element_by_name("session[username_or_email]")' Commented Sep 5, 2016 at 1:33
  • But this line only finding the element.. this exception can be occurred during interaction to the element like using as .clear() or send_keys().. Commented Sep 5, 2016 at 1:38

1 Answer 1

4

You should try using WebDriverWait to wait until element visible before interaction to the element as below :-

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)

user = wait.until(EC.visibility_of_element_located((By.NAME, "session[username_or_email]")))
user.clear()
user.send_keys(username)

pass = wait.until(EC.visibility_of_element_located((By.NAME, "session[password]")))
pass.clear()
pass.send_keys(password)

Note :- instead of send_keys(Keys.RETURN) try using click() to the login button element as login_button_element.click() or try using submit() to the form element as form_element.submit() after locating these elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.