1

Given that this is the element of the text box (Got this from Chrome inspect element https://www.mousehuntgame.com/login.php):

<input name="username" class="username" type="text" placeholder="Enter 
your username" onkeyup="app.pages.LoginPage.checkEnter(event, 'login');">

I am trying to input some text into the input box. I searched for the element by name, and used send_keys() to input some text into the text box

elem = driver.find_element_by_name("username")
elem.send_keys("aa")

I receive an exception:

ElementNotVisibleException: Message: element not interactable

I suspect i am not using find_element_by correctly. Where am I going wrong?

Additionally: Is there a way to tell in Chrome what I have selected from a find_element_by method? Is there some sort of method to highlight in the browser, the thing I have selected with my find_element_by method?

4
  • Try to wait for visibility of element. Also check whether there is only one input with "username" name: print(len(driver.find_elements_by_name("username"))) Commented Dec 11, 2018 at 10:18
  • @Gen Tan please upload the url of apge and full error log for the same. Commented Dec 11, 2018 at 10:26
  • @Andersson There was indeed more than 1 input with "username" name. How should I define my element more accurately since there is more than 1 input with "username"? Is there anyway to use driver.find_elements_by_name("username")[0] as an input? Commented Dec 11, 2018 at 10:29
  • @HimanshuPoddar added URL of page Commented Dec 11, 2018 at 10:37

3 Answers 3

4

There are two forms: first for Registration, second - for LogIn... Both have same input field with name "username"

You can use below CSS-selector to select input field in required form:

user_input_register = driver.find_element_by_css_selector("div.register input[name='username']")
user_input_login = driver.find_element_by_css_selector("div.login input[name='username']")

Or you can define forms for further input fields handling:

register = driver.find_element_by_class_name('register')
login = driver.find_element_by_class_name('login')

username_register = register.find_element_by_name('username')
username_login = login.find_element_by_name('username')
Sign up to request clarification or add additional context in comments.

7 Comments

Opss... didn't see your answer... for some reason, I found it easier to use XPath can you tell me what is the advantage of css_selector?
@MosheSlavin , in this current case "div.register input[name='username']" is just shorter :) XPath for the same will look like //div[contains(@class, "register")]//input[@name="username"]
Ok so it doesn't mean it will take longer to locate or that xpath is less robust...
@MosheSlavin , well CSS seem to be faster than XPath, but I'm not sure that you really can see that difference in performance. As for me XPath is more flexible and powerful, so I mostly use XPath
Thanks for the ref. I'll take a look and thanks again for your time!
|
1

As @Andersson commented you have more than one element with "username".

You should use a more specific XPath such as:

elem = driver.find_element_by_xpath('//input[@placeholder="Enter your username"]')

Or

elem = driver.find_element_by_xpath('//input[@class="username" and @type="text"]')

Comments

1

To send a character sequence to the username field with placeholder text as Enter your username, you have to induce WebDriverWait for the element to be clickable and you can use the following solution:

  • Code Block:

    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
    
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.username[name='username'][placeholder='Enter your username']"))).send_keys("Gen Tan")
    

Comments

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.