如何解决无法使用Selenium和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。您可以使用链接文本或部分链接文本。您可以参考此tutorial。您也可以尝试将xpath与该定位符“ // * [contains(text(),'忘记密码')]”
,您的xpath可以很简单:
“ // input [id ='用户名']”
elem=WebDriverWait(browser,10).until(EC.presence_of_element_located((By.XPATH,"//input[@id='username']")))
elem.send_keys("test")
您可以使用try and catchs测试它是否存在。
,对于您而言,我认为id或xpaht可以很好地找到用户名输入框。
browser.find_element_by_xpath("//input[@id='username']").send_keys("test")
因此,如果仍然失败,请提供更多信息,例如运行错误日志
,要将字符序列发送到元素而不是presence_of_element_located()
,您必须为element_to_be_clickable()
引入WebDriverWait,并且可以使用以下任一{ {3}}:
-
使用
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。