1

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.

4
  • 1) Please state what is not working. Is it not finding the element? Or is it not inputting the expected value? If it is the latter, we do not see your minimal example of inputting a value. Commented Jun 22, 2021 at 2:39
  • There is no element finding, this is the 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"} Commented Jun 22, 2021 at 2:41
  • It's going to be hard for anyone to help you if you don't also provide a link Commented Jun 22, 2021 at 2:43
  • This is the link : 123.60.12.11:10016/#/… Commented Jun 22, 2021 at 2:46

1 Answer 1

3

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

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

2 Comments

I already tried to used it by class name, but it's not working. This is the error : selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".username"}
You might need to add a wait to wait until the element is visible, as the page is slow to load. I updated my answer with working code, tested on your website. Let me know if it helps!

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.