2

I've been trying to reference the two inputs in the following HTML code using Selenium's get_element_by_...() with the purpose of automatically entering a username and password into the fields. I'm running into trouble with accessing the proper HTML elements.

<body>
  <div id="app">
    <div>
      <section id="root" class="page-login">
        <section class="main">
          <section class="auth-page register-fill">
            <div class="page-center">
              <a class="switch-lang" href="/cn/login">cn</a>
              <a class="btn-back" href="en/login">
                <span class="isvg loaded">
                  <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 24 24">
                    <path class="a" d="M0,0H24V24H0Z"></path>
                    <path class="b" d="M20,11H7.83l5.59-5.59L12,4,4,12l8,8,1.41-1.41L7.83,13H20Z"></path>
                  </svg>
                </span>
              </a>
              <div class="auth-page-wrapper">
                <h1 class="auth-page-title">Welcome Back</h1>
                <h2 class="auth-page-subtitle">Login to get started</h2>
                <div class="auth-page-fields">
                  <div class="custom-input">
                    <input type="text" placeholder="Email">
                    <label>Email</label>
                  </div>
                  <div class="custom-input custom-input-password">
                    <a class="forgot-link" href="en/forgot-password">Forgot Password?</a>
                    <input type="password" placeholder="Minimum 6 characters">
                    <label>Password</label>
                  </div>
                </div>

Note: The HTML may have some mistakes, I just quickly formatted it, but the mistakes should not affect the question.

Tried:

I've tried using all the different functions of the 'form find_element_by_...' and referencing a variety of things, but all tend to return "no such element: unable to locate element ..." I've done absolute xpaths, relative xpaths, css selectors, ids, and combinations of these.

Some examples:

username = browser.find_element_by_xpath("//input[1]")
username = browser.find_element_by_xpath("//html/body/div/div/section/.../input")

The error begins at this point in the path:

username = browser.find_element_by_xpath("//html/body/div/div/section")

I'm likely making a stupid mistake, so sorry if this question isn't great, but other StackOverflow answers all have ids and such that are referable on the input field. (eg. <input id=username ...>)

2 Answers 2

2

To identify the Email field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Email']")))
    
  • Using XPATH:

    username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Email']")))
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked. I thought it was somehow a syntax error, but I guess it was just a waiting issue. So the reason you need this is to account for the webpage loading time I assume?
0

There are more than one way to do this. But, this should work browser.find_element_by_css_selector('[placeholder="Email"]')

2 Comments

This is one of the things I'd already tried and it did not work. Thanks though.
I ran this code with the minimal html you have posted and it works. Maybe you need to wait for the web element to be visible like @DebanjanB has suggested. Or, you might be dealing with iframes in which case you have to switch to the specific frame in which your element exists.

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.