Ayoub M'HAREK
Ayoub M'HAREK

Reputation: 13

Fill username and password in input tag using selenium in python

I have elements in my code like this :

<input data-v-d6c12922="" type="text" name="account" placeholder="账号" class="username">  
<input data-v-d6c12922="" type="password" name="password" placeholder="密码" class="password">

I used this, but it's not working.

userElem = browser.find_element_by_xpath("//html/body/div[1]/div[1]/div[2]/div/div[2]/input")

Error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//html/body/div[1]/div[1]/div[2]/div/div[2]/input"}

How can I auto-fill the username and password?

Thank you.

Upvotes: 1

Views: 939

Answers (1)

AlbertGromek
AlbertGromek

Reputation: 580

The issue might be you are trying to find the element before it is visible.

You can wait for the element to be visible using the code below (I am also finding the ID by CLASS_NAME instead of XPATH, and capturing a screenshot to show the fields have been filled out)

driver.get("http://123.60.12.11:10016/#/login?url=http%3A%2F%2F123.60.12.11%3A10016%2F%23%2FMySociety%2FSocietyDetail")

wait = WebDriverWait(driver, 10)
username = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "username")))
username.send_keys("[email protected]")
pw = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "password")))
pw.send_keys("asdasd")

driver.get_screenshot_as_file("screenshot.png")

Working example

Upvotes: 3

Related Questions