gpt4 book ai didi

python - 如何通过Selenium WebDriver和Python定位用户名和密码元素

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:38 25 4
gpt4 key购买 nike

我正在尝试使用 Selenium 自动输入特定网站的用户名和密码。我无法找到要将 key 发送到的元素。我在下面附上了该网站的 HTML 代码。

这是我目前使用的:

username = driver.find_element_by_id('UserName').getAttribute('value')

此外,该网站以 .bsd 结尾,我并不完全熟悉它,并认为它可能是导致问题的原因。

谢谢。

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="css/general.css" type="text/css">
<title>RC Inspection - Risk Control</title>
<script language="javascript1.3" src="scripts/clientside/badgerAPI.js"></script>
<script language="javascript1.3" src="scripts/clientside/inspectionClient.js"></script>
<script language="javascript1.3" src="scripts/clientside/validate.js"></script>
<script language="javascript1.3" src="scripts/widJETs/JETClient.js"></script>
<script language="javascript1.3" src="scripts/widJETs/JETDataTable.js"></script>
<script>var searchParameters;</script>
</head>
<frameset rows="0,95,*" framespacing="0">

<frame name="SystemWin" src="about:blank" bgcolor="#FFFFFF" marginwidth="0" marginheight="0" frameborder="0" framespacing="0" scrolling="auto">
<frame name="HeaderWin" src="about:blank" bgcolor="#FFFFFF" marginwidth="0" marginheight="0" frameborder="0" framespacing="0" scrolling="no">
<frame name="MainWin" src="Logon.bsd?Lang=English'" marginwidth="0" marginheight="0" frameborder="0" framespacing="0" scrolling="auto"
<html><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script language="javascript1.3" src="scripts/clientside/XHConn.js"></script>

<link rel="stylesheet" href="css/logon.css" type="text/css">

<script language="javascript1.3">

function executeAction(sFunction){

if (sFunction=='OnLoad') {
this.document.MainForm.UserName.focus();
hide('r5');
}
else if (sFunction=='Login') {

fParms='UserName,T,50,1|Password,T,20,1';

if(top.validatePage('top.MainWin.document.MainForm',fParms,'English'))
top.goPage('LogonUser@Logon.bsd?form=top.MainWin.document.MainForm|target=SystemWin|Lang=English') ;
}
else if (sFunction=='ForgotPassword') {
hide('r1');
hide('r2');
show('r5');
}
else if (sFunction=='ResetPassword') {
if(top.MainWin.document.MainForm.RP_UserName.value == "") {
alert('Please enter Your Username');
return false;
}
var r = confirm("Are you sure you wish to reset your password?");
if (r == true) {
try {
var myConn = new XHConn();
if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
myConn.connect("ResetPassword@Utilities.bsd", "POST", "RP_UserName="+this.document.MainForm.RP_UserName.value+"&Lang=English", fn_ChangeItem);
}
catch(e) {
var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
alert("Unable to get XML data:\n" + msg);
return;
}
}
}
}

fn_ChangeItem = function (XML) {
eval(XML.responseText) ;
}
hide=function (szDivID) {
if(document.layers) //NN4+
eval("document."+szDivID+".visibility = 'hide'");
else if(document.getElementById) //gecko(NN6) + IE 5+
{
var obj = document.getElementById(szDivID);
obj.style.display = "none";
}
else if(document.all) // IE 4
document.all[szDivID].style.display = "none";
}
show=function (szDivID) {
if(document.layers) //NN4+
eval("document."+szDivID+".visibility = 'show'");
else if(document.getElementById) //gecko(NN6) + IE 5+
{ var obj = document.getElementById(szDivID);
obj.style.display = "";
}
else if(document.all) // IE 4
document.all[szDivID].style.display = "";
}
</script>
</head>
<body onload="javascript:executeAction('OnLoad');" marginwidth="0" marginheight="0">

<div class="loginwrapper">

<div class="logoncontent">
<div id="logon_header">
<img width="341" border="0" src="./images/companylogo.gif">

<div class="logintop">
Risk Control System <br>
version 4.5.4.1.5
</div>
</div>
<div class="clear">&nbsp;</div>
<div id="logon_content">
<form name="MainForm" enctype="multipart/form-data">

<div id="r1">
<div style="float:left;margin-right:20px;">
<label for="UserName">User Name</label>
<input type="text" id="UserName" name="UserName" maxlength="50" tabindex="1">
</div>
<div style="float:left;">
<label for="Password">Password</label>
<input type="password" id="Password" name="Password" maxlength="20" tabindex="2" onkeypress="if (event.keyCode=='13') executeAction('Login');">
</div>
</div>

<div class="clear">&nbsp;</div>

<div id="r2">
<a class="small" href="javascript:void(0);" onclick="executeAction('ForgotPassword');return false;">Forgot Password?</a>
<span class="action_button green_button"><a href="javascript:void(0);" tabindex="3" onclick="executeAction('Login');">Login</a></span>
</div>

<div id="r5" style="display: none;">
<label class="rp_label" for="RP_UserName">Please Enter Your Username</label>
<input type="text" name="RP_UserName" value="" size="30" maxlength="50">&nbsp;&nbsp;
<!-- <a href="javascript:void(0);" onclick="executeAction('ResetPassword');return false;">Reset Password</a> -->
<input type="submit" value="Reset Password" onclick="executeAction('ResetPassword');return false;">
</div>
</form>
</div>
</div>

<div class="clear">&nbsp;</div>

<div class="loginbottom">
<a style="text-decoration: none;" href="http://www.riskcontroltech.com/" target="_blank">Risk Control Technologies Inc.</a> Copyright © 2015. All rights reserved.<br>
All Trademarks are owned by their respective companies.
</div>
</div><!-- end loginwrapper -->



</body></html>
</frameset>


</html>

最佳答案

只需使用WebDriverWait:

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

driver = webdriver.Chrome(executable_path=r'C:\path\To\chromedriver.exe')
driver.get(url)

wait = WebDriverWait(driver, 10)

username = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@id='UserName']")))

# username = driver.find_element_by_xpath("//input[@id='UserName']")
username.send_keys("the useName")

# password = driver.find_element_by_xpath("//input[@id='Password']")

password =wait.until(EC.presence_of_element_located((By.XPATH, "//input[@id='Password']")))
password.send_keys("yourPassword")

希望对你有帮助!

关于python - 如何通过Selenium WebDriver和Python定位用户名和密码元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54077539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com