Selenium在页面中存在的元素上找不到元素

本文关键字:元素 找不到 存在 Selenium | 更新日期: 2023-09-27 17:58:31

public class Sele
   {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;
        public Sele( )
        {
            driver = new FirefoxDriver( );
            baseURL = "https://www.youtube.com/";
            verificationErrors = new StringBuilder( );
        }
        internal void LikeYoutubeVidWithAcc( string username, string password, string url )
        {
            driver.Navigate( ).GoToUrl( baseURL + "/" );
            driver.FindElement( By.XPath( "(//button[@type='button'])[2]" ) ).Click( );
            driver.FindElement( By.Id( "Email" ) ).Clear( );
            driver.FindElement( By.Id( "Email" ) ).SendKeys( "UserName" );
            driver.FindElement( By.Id( "next" ) ).Click( );
            driver.FindElement( By.Id( "Passwd" ) ).Clear( );
            driver.FindElement( By.Id( "Passwd" ) ).SendKeys( "Password" );
            driver.FindElement( By.Id( "signIn" ) ).Click( );
            driver.Navigate( ).GoToUrl( url );
            driver.FindElement( By.XPath( "(//button[@type='button'])[25]" ) ).Click( );
        }
}

我在使用selenium登录youtube时遇到问题,由于某种原因,它在页面中找不到密码元素。

错误发生在以下行:

 driver.FindElement( By.Id( "Passwd" ) ).Clear( );

错误消息:

An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll
Additional information: Unable to locate element: {"method":"id","selector":"Passwd"}

我知道页面上有元素,但硒找不到。

错误发生时的页面来源:

<div class="card signin-card      pre-shift         no-name shift-form">
  <div id="cc_iframe_parent"><iframe id="youtube" src="https://accounts.youtube.com/accounts/CheckConnection?pmpo=https%3A%2F%2Faccounts.google.com&amp;v=-951713550&amp;timestamp=1469398858532" style="visibility: hidden; width: 1px; height: 1px; position: absolute; top: -100px;"></iframe></div>
  <div class="circle-mask" style="background-image: none;">
  <canvas id="canvas" class="circle" width="96" height="96"></canvas>
  </div>
  <form novalidate="" method="post" action="https://accounts.google.com/signin/challenge/sl/password" id="gaia_loginform">
  <input name="Page" value="PasswordSeparationSignIn" type="hidden">
  <input name="GALX" value="bX35kb1ARtQ" type="hidden">
  <input name="gxf" value="AFoagUWY2FAFLKhmD2_-vufme-BJ1jfACg:1469398858433" type="hidden">
  <input name="continue" value="https://www.youtube.com/signin?next=%2F&amp;hl=en&amp;feature=sign_in_button&amp;app=desktop&amp;action_handle_signin=true" type="hidden">
  <input name="service" value="youtube" type="hidden">
  <input name="hl" value="en" type="hidden">
  <input id="profile-information" name="ProfileInformation" value="APMTqulOYcx8i4BIKa7QX8vzLf8nHVle5wmAmPaLqZqa7bjp6poGeUD2zvzYvR46xap0wdwr2kQaxUuteML1f-hTwb_VW4ZDLaD0cYYzVup4THWBRGWMWv0" type="hidden">
  <input id="_utf8" name="_utf8" value="☃" type="hidden">
  <input name="bgresponse" id="bgresponse" value="!DwylDC1CEBfic73NLmFEPmLuo0jLmEwCAAAAH1IAAAAFCgASXF5p4wpYu2Whl5kDy5GtZSOFmQEOdst3NYj0yd4fKBLZxl94_2g92h7yeFimnFaHcZosa1A6tveg3lkaz-HEnxsgWWDRAptp3hXbMHf10f_l5URE4mup4YdMKniavQDRHw1mYbNpaTFcVbTQhb2Fq8zAY89pRskujAVj_FVIMin3uw8U3Ncz0sjSkkSBbSi7TneLgUGXSI-GHNUS6uVJGKfqXUCSMkdDA2d71bq-oANdlKwxKOyCAAIkcNAwwuROjGY1wiWaMRofROyHO3PfRvpdwtKP0MfCUCvFU88cdyXKHt-xyJO76CR96U3E5loZGOFscMs8BCy2KpyjeyE6yGHVcSAMFI9NgQfT0Gv5E4687eD__44iyMRuyd4xUYcetZAL" type="hidden">
  <input id="pstMsg" name="pstMsg" value="1" type="hidden">
  <input id="dnConn" name="dnConn" value="" type="hidden">
  <input id="checkConnection" name="checkConnection" value="youtube:254:1" type="hidden">
  <input id="checkedDomains" name="checkedDomains" value="youtube" type="hidden">
  <div class="form-panel first valid" id="gaia_firstform">
  <div style="transition-delay: 0ms;" class="slide-out hide-form">
  <div class="input-wrapper focused">
  <div id="identifier-shown"></div>
  <span role="alert" class="error-msg" id="errormsg_0_Email"></span>
  </div>
<div style="display: none" id="identifier-captcha">
<input name="identifiertoken" id="identifier-token" value="" type="hidden">
<input name="identifiertoken_audio" id="identifier-token-audio" type="hidden">
<div class="audio-box">
  <div id="playIdentifierAudio"></div>
</div>
<div id="captcha-box" class="captcha-box">
  <div id="captcha-img" class="captcha-img" data-alt-text="Visual verification"></div>
  <span class="captcha-msg">
  Letters are not case-sensitive
  </span>
</div>
<label for="identifier-captcha-input" class="hidden-label"></label>
<input id="identifier-captcha-input" name="identifier-captcha-input" class="captcha" placeholder="Enter the letters above" title="Type the characters you see or numbers you hear" type="text">
</div>
<input id="next" name="signIn" class="rc-button rc-button-submit" value="Next" type="submit">
  <a class="need-help" href="https://accounts.google.com/signin/recovery?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252F%26hl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue&amp;service=youtube&amp;ignoreShadow=0&amp;hl=en">
  Need help?
  </a>
  </div>
  </div>
  <a href="https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252F%26hl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue&amp;service=youtube&amp;hl=en" tabindex="-1">
  <img id="back-arrow" class="back-arrow shift-form" aria-label="Back" tabindex="0" alt="Back" src="https://www.gstatic.com/images/icons/material/system/1x/arrow_back_grey600_24dp.png">
  </a>
  <div class="form-panel second">
  <div style="transition-delay: 100ms;" class="slide-in">
  <div>
  <p style="display: none;" id="profile-name"></p>
  <span id="email-display">email123456@gmail.com</span>
  </div>
  <div>
  <div id="password-shown"><div>
  <input name="Email" id="Email-hidden" spellcheck="false" class="hidden" value="" readonly="" autocomplete="off" type="email">
<label class="hidden-label" for="Passwd">Password</label>
<input id="Passwd" name="Passwd" placeholder="Password" class="" type="password">
  </div></div>
  </div>
<input id="signIn" name="signIn" class="rc-button rc-button-submit" value="Sign in" type="submit">
  <label class="remember">
  <input id="PersistentCookie" name="PersistentCookie" value="yes" checked="checked" type="checkbox">
  <span>
  Stay signed in
  </span>
  <div class="bubble-wrap" role="tooltip">
  <div class="bubble-pointer"></div>
  <div class="bubble">
  For your convenience, keep this checked. On shared devices, additional precautions are recommended.
  <a href="https://support.google.com/accounts/?p=securesignin&amp;hl=en" target="_blank">Learn more</a>
  </div>
  </div>
  </label>
  <input name="rmShown" value="1" type="hidden">
  <a id="link-forgot-passwd" class="need-help" href="https://accounts.google.com/signin/recovery?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252F%26hl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue&amp;service=youtube&amp;checkedDomains=youtube&amp;checkConnection=youtube%3A254%3A1&amp;pstMsg=1&amp;Email=wageeh14032%40gmail.com&amp;ignoreShadow=0&amp;hl=en">
  Forgot password?
  </a>
  </div>
  </div>
  <span id="inge" style="display: none" role="alert" class="error-msg">
  Sorry, Google doesn't recognize that email. <a href="https://accounts.google.com/SignUpWithoutGmail?service=youtube&amp;continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252F%26hl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue&amp;hl=en">Create an account</a> using that address?
  </span>
  <span id="timeoutError" style="display: none" role="alert" class="error-msg">
  Something went wrong. Check your connection and try again.
  </span>
  </form>
</div>

Selenium在页面中存在的元素上找不到元素

试试这个:

WebDriverWait wait = new WebDriverWait(driver, 10); //you can change 10 seconds to whatever it suits you the best.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));