3

I am using Selenium Webdriver to login to a site. I've tried multiple different selectors, and have tried implicit waits, but cannot locate the element.

    from selenium import webdriver
    from selenium.webdriver.common.by import By

    browser = webdriver.Firefox()
    url = "https://www.example.com"
    login_page = browser.get(url)

    username = browser.find_element_by_id("Email")
    # Also tried:
    # username = browser.find_element_by_xpath('//*[@id="Email"]')
    # username = browser.find_element_by_css_selector('#Email')
    username.send_keys("email")

This is the html

<div class="form-group">
    <label for="Email">Email address</label>
    <div class="input-group" style="width: 100%">
        <input class="form-control email" data-val="true" data-val-length="Maximum length is 50" data-val-length-max="50" data-val-regex="Provided email address is not valid" data-val-regex-pattern="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" data-val-required="Email is required" id="Email" name="Email" type="email" value=""><br>
        <span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
    </div>
</div>

Here is the error message

Traceback (most recent call last):
  File "seleniumloginpi.py", line 12, in <module>
    email.send_keys('email')
  File "/Users/greg/anaconda/envs/trade/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 320, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
  File "/Users/greg/anaconda/envs/trade/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 461, in _execute
    return self._parent.execute(command, params)
  File "/Users/greg/anaconda/envs/trade/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/Users/greg/anaconda/envs/trade/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, 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:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/[email protected]/components/command-processor.js:10092)
    at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/[email protected]/components/command-processor.js:12644)
    at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/[email protected]/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/[email protected]/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/[email protected]/components/command-processor.js:12608)

Any help would be greatly appreciated.

1
  • Are you sure there is only one element with the id email?? Commented Aug 7, 2016 at 22:10

1 Answer 1

1

Actually you are locating element, problem with the send_keys, here could not be set value on the email input due to invisibility of element. But as I see in provided HTML no style attribute property exists on email input element which could make it invisible.

May be possible there are more elements with the same id and you are interacting with other element, you should try with some different locator as below :-

username = browser.find_element_by_css_selector('div.input-group input#Email.form-control.email')
username.send_keys("email") 

Or try to find all elements with the Id Email and perform send_keys() on visible element as below :

usernames = browser.find_elements_by_id('Email')

for username in usernames:
   if username.is_displayed():
        username.send_keys("email") 
        break
Sign up to request clarification or add additional context in comments.

2 Comments

There was a second element with the id "Email". Couldn't solve it by using the css selector but looping worked. Thank you!!
@gtownrower without looping also would work if you will try to provide unique locator, I think this xpath may be unique .//label[text() = 'Email address']/following::input[@id = 'Email'] or (.//input[@id = 'Email'])[2]..

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.