无法在 python 中使用 selenium 定位元素(隐藏文本框)

Unable to locate element (hidden textbox) using selenium in python

NID文本框中传递值后,新元素显示在网页上。例如 Firstname :

for row in rows:
    y = driver.find_element_by_id('NId')
    y.send_keys(row)
    driver.find_element_by_xpath("//body").click()
    
    a1 = driver.find_element_by_id('Firstname')
    firstname = a1.get_attribute('value')

执行命令设置a1后,报错:

Exception has occurred: NoSuchElementException Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="Firstname"]"}

文本框 NId :

<input class="form-control ltr left text-box single-line" data-val="true" data-val-regex="It is 10 numbers" data-val-regex-pattern="^[0-9]{10}$" data-val-required="*required" id="NId" maxlength="10" name="NId" onblur="LoadInfo()" type="text" value="">

Textobx名称(填写NId后出现):

<form action="/Employees/Manager/SavePerson" data-ajax="true" data-ajax-begin="onpostcreatebegin" data-ajax-complete="onpostcreatecomplete" data-ajax-loading="#ajaxloading" data-ajax-method="post" data-ajax-mode="replace" data-ajax-update="#result" id="form0" method="post" novalidate="novalidate"><input name="__RequestVerificationToken" type="hidden" value="asdxBadsP7CpS53654as6dadH3865asdadKhjasdad">        <input type="hidden" name="empId" id="empid" value="0">
    <div class="panel panel-info">
        <div class="panel-body">
            <input data-val="true" data-val-regex="It is 10 numbers" data-val-regex-pattern="^[0-9]{10}$" data-val-required="*required" id="NId" name="NId" type="hidden" value="1234567890">
            <input data-val="true" data-val-number="The field PersonId must be a number." data-val-required="The PersonId field is required." id="PersonId" name="PersonId" type="hidden" value="254102232">
            <input data-val="true" data-val-required="The GuidId field is required." id="GuidId" name="GuidId" type="hidden" value="665xs6asd-cxc2-wq56-8888-30654998b166">
            <div class="form-group">
                <div class="col-md-4">
                    <input class="form-control text-box single-line" data-val="true" data-val-regex="enter the name." data-val-regex-pattern="^[\u0600-\u06ff\s]+$|[\u0750-\u077f\s]+$|[\ufb50-\ufc3f\s]+$|[\ufe70-\ufefc\s]+$|[\u06cc\s]+$|[\u067e\s]+$|[\u06af\s]$|[\u0691\s]+$|^$" data-val-required="*required" id="Firstname" name="Firstname" type="text" value="jack">
                    <span class="field-validation-valid text-danger" data-valmsg-for="Firstname" data-valmsg-replace="true"></span>
                </div>
            </div>

        </div>
    </div>  
</form>

我想元素需要时间才能显示,所以请使用 explict 等待。

代码:

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "Firstname")) 
firstname = element.get_attribute('value')
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver,100)

locate = wait.until(EC.presence_of_element_located((By.XPATH,'your_xpath_here')))
locate_value = locate.text