简体   繁体   中英

Unable to locate an input box element using Selenium and Python

So I have been trying to locate the username input box on python using selenium. I have tried xpath, id, class name, etc.

I am also aware of explicit wait for the elements to load. However, depite all this, no luck. I checked to see if the element is in an iframe that I overlooked, but I couldn't find it.

Here is the user input box element.

<div class="panel-body">
                    <h1 class="text-normal" role="banner">Sign in to your account</h1>


                    <form action="/idp/profile/SAML2/Redirect/SSO?execution=e1s1" method="post">
                        <legend>
Login to Qualtrics - co1
                        </legend>
                                        
                        <div class="form-group">
                          <label for="username" class="hidden">Username</label>
                          <div class="input-group">
                            <input id="username" name="j_username" type="text" class="form-control" tabindex="0" placeholder="Username" autocorrect="off" autocapitalize="none" spellcheck="false" aria-label="Username">
                            <div class="input-group-addon">@manhattan.edu</div>
                          </div>
                        </div>
                        <div class="form-group">
                          <label for="password" class="hidden">Password</label>
                          <input id="password" value="" name="j_password" type="password" class="form-control" placeholder="Password" aria-label="Password" autocomplete="off">
                        </div>                         


                                                  <div class="form-element-wrapper">
                            <input type="checkbox" name="donotcache" value="1" id="donotcache">
                            <label for="donotcache">Don't Remember Login</label>
                          </div>

                      <div class="form-element-wrapper">
                        <input id="_shib_idp_revokeConsent" type="checkbox" name="_shib_idp_revokeConsent" value="true">
                        <label for="_shib_idp_revokeConsent">Clear prior granting of permission for release of your information to this service.</label>
                      </div>

                        <div class="form-element-wrapper">
                          <button class="btn btn-default" type="submit" name="_eventId_proceed" onclick="this.childNodes[0].nodeValue='Logging in, please wait...'">Login</button>
                        </div>

                      <div class="clearfix"></div>
                    </form>
                    
                      <img src="https://s.qualtrics.com/login/static/xm_logo-16.png" alt="Qualtrics - co1">
                    <hr>
                    <ul class="create-account">
                        <li><a href="https://start.manhattan.edu/"> Forgot your password?</a></li>
                      <li>New student? <a href="https://start.manhattan.edu/">Get your JasperNet ID and password.</a></li>
                      <li><a href="https://inside.manhattan.edu/offices/its/client-services-hours-and-support.php"> Need Help?</a></li>
                    </ul>
                    </div>

Here is the code that I wrote up to locate the element.

WebDriverWait(browser,10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div/div/div/div/div[3]/div[1]/div[1]/div/ul/li[1]/a')))
browser.find_element_by_xpath('/html/body/div/div/div/div/div[3]/div[1]/div[1]/div/ul/li[1]/a').send_keys("test")

You are using an absolute xpath which is not advisable. You can use link text or partial link text. You can refer to this tutorial . You can also try using xpath with this locator "//*[contains(text(),'Forgot your password')]"

Your xpath can be simply:

"//input[id='username']"

elem=WebDriverWait(browser,10).until(EC.presence_of_element_located((By.XPATH, "//input[@id='username']")))
elem.send_keys("test")

You could use try and catches to test if it's present.

I think, in your case, id or xpaht is great to locate the username input box.

browser.find_element_by_xpath("//input[@id='username']").send_keys("test")

So, if you still fail, please provide more information, such as running error log

To send a character sequence to the element instead of presence_of_element_located() you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control#username"))).send_keys("test")
  • Using XPATH :

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control' and @id='username']"))).send_keys("test")
  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM