简体   繁体   English

使用 selenium python 填充或忽略所需的随机字段

[英]Fill or ignore required random field with selenium python

I am practicing with selenium to log into a website and grab the mortage ticket (print/save/download it as pdf).我正在练习使用 selenium登录网站并获取抵押票据(打印/保存/下载为 pdf)。

The logging page is as follows:登录页面如下:

Field 1: Contract number字段 1:合同编号

Field 2: National user-id字段 2:国家用户 ID

Botton: Botton to validate the contract Botton:用于验证合约的Botton

Field 3 : Requests some aleatory personal info (day of birh, mother's name, zip code, National Health ID, voter registration etc.) each time the page is accessed/refreshed字段 3 :每次访问/刷新页面时,请求一些偶然的个人信息(出生日期、母亲姓名、zip 代码、国民健康 ID、选民登记等)

When I log to it (every month) throughout the user page, when it comes to some aleatory info that I don't know by heart (such as National Heatlh ID or voter registration), I refresh the page until it brings me some easy to remember info(phone number, zip code, etc.).当我在整个用户页面(每个月)登录它时,当涉及到一些我不记得的偶然信息(例如 National Heatlh ID 或选民登记)时,我刷新页面直到它给我带来一些简单的信息记住信息(电话号码、zip 代码等)。

What can I do to go through this last field that requests personal information in a random way(some that I know by heart, others I do not)?我可以通过最后一个以随机方式请求个人信息的字段对 go 做什么(有些我知道,有些我不知道)?

The website code referring to the 3rd field is as follows.涉及第3个字段的网站代码如下。 The parameters name="zipCode" and placeholder="Zip Code" parts are the ones that keeps changing their values each time the page is refreshed.参数name="zipCode"placeholder="Zip Code"部分是每次刷新页面时不断更改其值的部分。

input id="aleatoryField" class="mat-input-element mat-form-field-autofill-control 
ng-tns-c4-1 cdk-text-field-autofill-monitored ng-dirty ng-invalid ng-touched" 
_ngcontent-iqu-c4="" autocomplete="off" 
formcontrolname="aleatoryField" inputmode="decimal" matinput=""
pattern="[0-9]*" required="" name="zipCode" placeholder="Zip Code"
aria-describedby="mat-error-2" aria-invalid="true" aria-required="true"

input id="aleatoryField" class="mat-input-element mat-form-field-autofill-control ng-tns-c4-1 cdk-text-field-autofill-monitored ng-dirty ng-invalid ng-touched" 
_ngcontent-iqu-c4="" autocomplete="off" 
formcontrolname="aleatoryField" inputmode="decimal" matinput=""
pattern="[0-9]*" required="" name="voterId" placeholder="Voter Registration"
aria-describedby="mat-error-2" aria-invalid="true" aria-required="true"

input id="aleatoryField" class="mat-input-element mat-form-field-autofill-control ng-tns-c4-1 cdk-text-field-autofill-monitored ng-dirty ng-invalid ng-touched" 
_ngcontent-iqu-c4="" autocomplete="off" 
formcontrolname="aleatoryField" inputmode="decimal" matinput=""
pattern="[0-9]*" required="" name="yearBirth" placeholder="Year of Birth"
aria-describedby="mat-error-2" aria-invalid="true" aria-required="true"


# MY CURRENT CODE

from selenium import webdriver

driver = webdriver.Firefox(executable_path="./geckodriver.exe" )

# open the website
driver.get("https://www.habitacaodigital.caixa.gov.br/acesso-cliente")

# FIELD 1: contract info
contract = driver.find_element_by_id("contract")
national_id = driver.find_element_by_id("nationalId")

# FIELD 2: filling the contract info
contract.send_keys("123")
national_id.send_keys("321")

# botton
validate_contract = driver.find_element_by_id("validate_contract")
validate_contract.click()

# FIELD 3: aleatory personal info field (cellphone number, zip code, day of birth, mother's name...)
# aleatory_field = driver.find_element_by_id("aleatoryField")
# aleatory_field.send_keys("HOW TO DEAL WITH THIS PART OVER HERE?")

# undetected Selenium suggestion
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#aleatoryField[formcontrolname='aleatoryField']"))).send_keys("234567")

# LOGGING BOTTON
btn_logging = driver.find_element_by_id("btn_access")
btn_logging.click()

Result with undetected Selenium first suggestion added in未检测到的结果 Selenium 添加了第一个建议结果

It seems you have no problem with dynamic element since element id was never changed.似乎您对动态元素没有问题,因为元素id从未更改过。 you have problem with input values based on type of the field name appeared on the screen on each login.您对基于每次登录时屏幕上显示的字段name类型的输入值有疑问。

You can use python switch case and configured all return type attribute name and then call the function and if it matches the specific name then execute that block only with your input.您可以使用python switch case并配置所有返回类型attribute name ,然后调用 function,如果它与特定name匹配,则仅使用您的输入执行该块。

def SetInputValue(element, elementAttVal):
    match elementAttVal:
        case "zipCode":
            element.send_keys("400910")
        case "voterId":
             element.send_keys("1234588999")
        case "yearBirth":
            element.send_keys("1990")     
            
//identify the element 
element=WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#aleatoryField")))
//Get the name attribute value
elementAttVal=element.get_attribute("name")
print(elementAttVal)
//Set the input based on name attribute
SetInputValue(element,elementAttVal)

Update : with if..block like that you have add the remaining config as well更新:使用 if..block 这样你也添加了剩余的配置

def SetInputValue(element, elementAttVal):
        if elementAttVal=="zipCode":
                element.send_keys("400910")
        if elementAttVal=="voterId":
                element.send_keys("1234588999")
        if elementAttVal=="yearBirth":
                element.send_keys("1990")     
                
    //identify the element 
    element=WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#aleatoryField")))
    //Get the name attribute value
    elementAttVal=element.get_attribute("name")
    print(elementAttVal)
    //Set the input based on name attribute
    SetInputValue(element,elementAttVal)

To send a character sequence to the <input> element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies :要将字符序列发送到<input>元素,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#aleatoryField[formcontrolname='aleatoryField']"))).send_keys("234567")
  • Using XPATH :使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='aleatoryField' and @formcontrolname='aleatoryField']"))).send_keys("234567")
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM