繁体   English   中英

无法使用 Selenium 和 Python 定位输入框元素

[英]Unable to locate an input box element using Selenium and Python

所以我一直在尝试使用 selenium 在 python 上定位用户名输入框。 我尝试过 xpath、id、类名等。

我也知道显式等待元素加载。 然而,尽管这一切,没有运气。 我检查了该元素是否在我忽略的 iframe 中,但我找不到它。

这是用户输入框元素。

<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>

这是我编写的用于定位元素的代码。

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")

您正在使用绝对不可取的 xpath。 您可以使用链接文本或部分链接文本。 你可以参考这个教程 您也可以尝试使用带有此定位器的 xpath "///*[contains(text(),'Forgot your password')]"

您的 xpath 可以很简单:

"//输入[id='用户名']"

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

您可以使用 try 和 catches 来测试它是否存在。

我认为,在您的情况下,id 或 xpaht 非常适合定位用户名输入框。

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

所以,如果你仍然失败,请提供更多信息,例如运行错误日志

要将字符序列发送到元素而不是presence_of_element_located()您必须为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • 使用CSS_SELECTOR

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

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control' and @id='username']"))).send_keys("test")
  • 注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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