无法在用户名和密码输入字段

时间:2018-01-05 06:31:52

标签: python-3.x selenium selenium-webdriver automation automated-tests

我正在编写用于使用凭据自动执行登录操作的脚本。我遵循通过名称,类,xpath查找元素的标准过程。输入字段未获取凭据。我尝试在每个进程之间给出10秒的时间间隔,但问题仍然存在。

下面我已经证明了代码。

from selenium import webdriver
from selenium.webdriver.common.by import  By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
import time


driver = webdriver.Firefox()

driver.get()   #WEBSITE HERE

time.sleep(10)

elem = driver.find_element_by_xpath('//*[@id="ssousernameUI"]')

elem.clear()
elem.send_keys()  # USERNAME HERE

elem.send_keys(Keys.RETURN)

time.sleep(10)

passwd = driver.find_element_by_xpath('//*[@id="password"]')

passwd.clear()

passwd.send_keys()  #PASSWORD HERE

passwd.send_keys(Keys.RETURN)

time.sleep(10)

login = driver.find_element_by_link_text("Login")

login.click()

错误消息:

  Traceback (most recent call last):
  File "/home/think201/PycharmProjects/pat/pat-cbt/login.py", line 13, 
  in <module>
elem = driver.find_element_by_xpath('//*[@id="ssousernameUI"]')


  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 368, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)



  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 858, in find_element
'value': value})['value']



  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
self.error_handler.check_response(response)




  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response



raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to 
locate element: //*[@id="ssousernameUI"]

HTML代码

&#13;
&#13;
          
         <form name="loginForm" action="https://login.asdfghjk.in/oam/server/auth_cred_submit" target="_top" id="loginForm" method="POST">   
   
<!-- Taksh End -->

<input name="ipRandomValue" value="709381701517861" id="ipRandomValue" type="hidden"><input name="deviceTypeHidden" value="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" id="deviceTypeHidden" type="hidden">
<input name="ssousername" value="" id="ssousername" type="hidden">
<input name="hashnumber" value="" id="hashnumber" type="hidden"> 


<!-- Start mainWrapper -->
<div class="overlay"></div>
<div style="top: 20%;left: 50%;position: fixed;z-index: 5;color: white;display:none;" class="wait">
    <h1>Please wait...</h1>
</div>

<div class="overlay1"></div>
<div class="mainWrapper"> 
  <!-- Start Header -->
  
  
  <!-- End Header --> 
  
  <!-- Start Middle Container -->
  <article class="midContainer">
    <div class="container">
      <div class="breadcrums">
        <p>You are here:</p>
        <ul>
          <li><a href="javascript:;">Home</a></li>
          <li class="last"> Login</li>
        </ul>
      </div>
      <h1> Login</h1>
      <section class="RHSsection standardWide">
        <div class="pageWrap">
          <div class="loginWrp">
            <div class="box vfAcc">
            
            <!-- displaying error message -->
            <div class="cl c414042 bold fnt16"><p style="color:red;margin-left:10px;"><br><strong class="errMsgClass"></strong></p></div>
            <div class="cl c414042 bold fnt16" id="SrvError"></div>
            <div style="display:none;" class="cl c414042 bold fnt16 lockClass"><p style="color:red;margin-left:10px;"><br><strong>Your account is locked since it is 
not used for a long time. Please <a href="https://qwertyu.in/CSSPortal/faces/oracle/webcenter/portalapp/pages/forgotPassword.jspx">click here </a> to activate it</strong></p></div>
               
            <!-- displaying error message end -->
              
              <h3 class="accor">Login to manage your Account <span class="drop"></span></h3>
              <div class="setWrp">
                <div class="row">
                  <label>Enter your username or mobile number</label>
                  <div class="inputArea">
                    <div class="inputFieldBox">
                    
                    
                     <input name="ssousernameUI" autocomplete="off" class="login_input width255" value="" onfocus="if
(this.value==this.defaultValue)this.value='';" id="ssousernameUI" onblur="if(this.value=='')this.value=this.defaultValue;" type="text">
                          <div class="error_msg"></div>                  
                    </div>
                 </div> 
                  
                </div>
                <div class="row">
                  <label>Enter your password</label>
                  <div class="inputArea">
                    <div class="inputFieldBox">
                      
                      <input name="password" autocomplete="off" id="password" class="login_input width255" onkeydown="if ((event.keyCode == 
13) &amp;&amp; (document.getElementById('ssousernameUI').value!='') &amp;&amp; (document.getElementById('passwordsrv').value!='')){LoginValidation();}" onblur="if(this.value=='')this.value=this.defaultValue;" value="" onfocus="if
(this.value==this.defaultValue)this.value='';" type="password">
                         <div class="error_msg"></div>                    
                    </div> 
                   </div>
                </div>
                <div class="row">
                  <div class="checkboxBtn"> <span class="uncheked"></span>
                    <input name="checkbox" id="RememberMe" type="checkbox">
                    <label for="RememberMe"> Remember Me </label>
                  </div>
                  <div class="forgot"> <a href="https://qwertyuio.in/CSSPortal/faces/oracle/webcenter/portalapp/pages/forgotPassword.jspx" onclick="ga('send', 'event', 'My Account', 'Click', 'Forgot Password'); utag.link({ Custom_Links : 'MyAccount: Login: Forgot Password' });">Forgot your password?</a></div>
                </div>
                
                <a href="javascript:;" onclick="ajaxLoginValidation();" class="purpleBtn">Login</a><!-- Add Here : Taksh -->
                
                
              <!--  <div class="col orline"><span class="or">Or</span></div>
                <ul class="smoShare">
                  <li class="facebook"><a href="javascript:;">Facebook</a></li>
                  <li class="gplus"><a href="javascript:;">Google Plus</a></li>
                </ul> -->
              </div>
            </div>
            <div class="box myvf">
              <h3 class="accor">Why should I register ? <span class="drop"></span></h3>
              <div class="setWrp">
                <p>Here are 5 great reasons! </p>
                <ul class="redbult">
                  <li>Simplified, dashboard view of voice &amp; Internet usage</li>
                  <li> Easy update of profile &amp; contact info</li>
                  <li> Quick change of voice &amp; plans</li>
                  <li> Hassle-free access to old bills &amp; statements of accounts</li>
                  <li> Instant activation/deactivation of services</li>
                </ul>
                <p>It takes just a few moments!</p>
                <a href="https://qwertyuio.in/CSSPortal/faces/oracle/webcenter/portalapp/pages/Registration.jspx" onclick="ga('send', 'event', 'My Account', 'Click', 'Register Now'); utag.link({ Custom_Links : 'MyAccount: Login: Register Now', Internal_Journey : 'MyAccount Login: My VF Registration'});" class="purpleBtn">Register Now</a>
                </div>
            </div>
            <div class="box vfApp">
              <h3 class=""> App</h3>
              <div class="">
                <div class="vfappSlide">
                  <div class="owl-vfapp owl-carousel owl-theme owl-loaded">
                    
                    
                <!--    <div class="apps"> <img src="/images/appimages/windows_logo.jpg"  alt="" />
                      <p>Manage your account on the go !</p>
                        <div class="getNow"><a onclick="ga('send', 'event', 'My Account', 'Click', 'Windows - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: windows App - Get Now' });" href="http://qwertyu" class="purpleBtn">Get Now</a> </div>
                    </div>
                    <div class="apps"> <img src="/images/appimages/blackberry_logo.png"  alt="" />
                      <p>Manage your Vodafone account on the go !</p>
                      <div class="getNow"><a onclick="ga('send', 'event', 'My Account', 'Click', 'Blackberry - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: Blackberry App - Get Now' });" href="http://qwerty" class="purpleBtn">Get Now</a> </div>
                      </div>
                      <div class="apps"> <img src="/images/appimages/nokia_ovi_logo.jpg"  alt="" />
                      <p>Manage your account on the go !</p>
                       <div class="getNow"><a onclick="ga('send', 'event', 'My Account', 'Click', 'OVI - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: Ovi App - Get Now' });" href="http://qwertyu" class="purpleBtn">Get Now</a> </div>                      
                       </div>
                      <div class="apps"> <img src="/images/appimages/java_logo.jpg"  alt="" />
                      <p>Manage your account on the go !</p>
                       <div class="getNow"><a onclick="ga('send', 'event', 'My Account', 'Click', 'Java - Get Now');utag.link({ Custom_Links : 'MyAccount: Login: Java App - Get Now' });" href="http://qwertyui" class="purpleBtn">Get Now</a> </div>
                       </div> -->
                  <div class="owl-stage-outer"><div class="owl-stage" style="transform: translate3d(-837px, 0px, 0px); transition: all 0.25s ease 0s; width: 1674px;"><div class="owl-item cloned" style="width: 279px; margin-right: 0px;"><div class="apps"> <img alt="" src="/images/appimages/android_logo1.jpg">
                      <p>Manage your Vodafone account on the go !</p>
                        <div class="getNow"><a href="http://asdfgh" onclick="ga('send', 'event', 'My Account', 'Click', 'Android - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: Android App - Get Now' });" class="purpleBtn">Get Now</a> </div>
                        </div></div><div class="owl-item cloned" style="width: 279px; margin-right: 0px;"><div class="apps"> <img alt="" src="/images/appimages/apple_logo1.jpg">
                      <p>Manage your Vodafone account on the go !</p>
                       <div class="getNow"><a href="http://qwertyu" onclick="ga('send', 'event', 'My Account', 'Click', 'Apple - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: iphone App - Get Now' });" class="purpleBtn">Get Now</a> </div>
                     
                    </div></div><div class="owl-item" style="width: 279px; margin-right: 0px;"><div class="apps"> <img alt="" src="/images/appimages/android_logo1.jpg">
                      <p>Manage your Vodafone account on the go !</p>
                        <div class="getNow"><a href="http://asdfghj" onclick="ga('send', 'event', 'My Account', 'Click', 'Android - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: Android App - Get Now' });" class="purpleBtn">Get Now</a> </div>
                        </div></div><div class="owl-item active" style="width: 279px; margin-right: 0px;"><div class="apps"> <img alt="" src="/images/appimages/apple_logo1.jpg">
                      <p>Manage your  account on the go !</p>
                       <div class="getNow"><a href="http://asdfghjk" onclick="ga('send', 'event', 'My Account', 'Click', 'Apple - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: iphone App - Get Now' });" class="purpleBtn">Get Now</a> </div>
                     
                    </div></div><div class="owl-item cloned" style="width: 279px; margin-right: 0px;"><div class="apps"> <img alt="" src="/images/appimages/android_logo1.jpg">
                      <p>Manage your  account on the go !</p>
                        <div class="getNow"><a href="http://asdfghjk" onclick="ga('send', 'event', 'My Account', 'Click', 'Android - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: Android App - Get Now' });" class="purpleBtn">Get Now</a> </div>
                        </div></div><div class="owl-item cloned" style="width: 279px; margin-right: 0px;"><div class="apps"> <img alt="" src="/images/appimages/apple_logo1.jpg">
                      <p>Manage your  account on the go !</p>
                       <div class="getNow"><a href="" onclick="ga('send', 'event', 'My Account', 'Click', 'Apple - Get Now'); utag.link({ Custom_Links : 'MyAccount: Login: iphone App - Get Now' });" class="purpleBtn">Get Now</a> </div>
                     
                    </div></div></div></div><div class="owl-controls"><div class="owl-nav"><div class="owl-prev" style="display: none;">prev</div><div class="owl-next" style="display: none;">next</div></div><div style="" class="owl-dots"><div class="owl-dot"><span></span></div><div class="owl-dot active"><span></span></div></div></div></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
    </div>
  </article>
  
  
  <!-- End Middle Container --> 
  
</div>
<!-- End mainWrapper -->
</form>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:1)

NoSuchElementException是因为webdriver无法在网页中找到该元素。检查网页中是否有任何框架。切换到它,如果有的话。

答案 1 :(得分:0)

您需要单击元素以使该元素聚焦,然后输入用户名/密码详细信息。 如果这也不起作用,请尝试使用显式等待包装find元素语句。 如果有帮助,请告诉我。

EDIT1:

elem.click();
elem.send_keys();
time.sleep(50);

EDIT2: 而不是:

elem = driver.find_element_by_xpath('//*[@id="ssousernameUI"]')

你可以尝试:

driver.find_element_by_xpath('//*[@id="ssousernameUI"]').click()

答案 2 :(得分:0)

第一眼看来,似乎你的xpath不正确。 而不是使用:

  

elem = driver.find_element_by_xpath('// * [@ id =“ssousernameUI”]')

试试这个:

  

elem = driver.find_element_by_xpath(“// * [@ id ='ssousernameUI']”)

答案 3 :(得分:0)

根据您分享的credentials来填充from selenium import webdriver driver = webdriver.Firefox() driver.get("your_website") #WEBSITE HERE username = driver.find_element_by_xpath("//input[@id='ssousernameUI']").click() username.clear() username.send_keys("user_name") # USERNAME HERE password = driver.find_element_by_xpath("//input[@id='password']").click() password.clear() password.send_keys("Pa$$w0rd") #PASSWORD HERE driver.find_element_by_xpath("//a[@class='purpleBtn' and contains(.,'Login')]").click() ,您可以使用以下代码块:

append

答案 4 :(得分:0)

谢谢。我解决了

该网站有多个登录页面,因此他们使用iframe来满足多个页面中的登录访问权限。

问题是,登录位于iframe内。我提到了切换框架的文档。 我用了

        driver.switch_to_frame("frameName")

谢谢@Arun Prakash和大家。