用 selenium python 填充或忽略所需的随机字段
Fill or ignore required random field with selenium python
我正在练习使用 selenium 登录网站 并获取抵押票据(print/save/download pdf 格式)。
登录页面如下:
字段1:合同号
字段 2:国家用户 ID
Botton:用于验证合约的Botton
字段 3:每次页面 accessed/refreshed
当我在整个用户页面(每个月)登录它时,当涉及到一些我不记得的偶然信息(例如国家健康 ID 或选民登记)时,我刷新页面直到它给我带来了一些容易记住的信息(phone 号码、邮政编码等)。
我该怎么做才能通过最后一个以随机方式请求个人信息的字段(有些我知道,有些我不知道)?
第三个字段的网站代码如下。参数 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()
添加了未检测到的 Selenium 第一个建议的结果
要将 字符序列 发送到 <input>
元素,您需要引入 WebDriverWait for the and you can use either of the following :
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#aleatoryField[formcontrolname='aleatoryField']"))).send_keys("234567")
使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='aleatoryField' and @formcontrolname='aleatoryField']"))).send_keys("234567")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您似乎对动态元素没有问题,因为元素 id
从未更改过。您遇到基于字段类型的输入值问题 name
出现在每次登录的屏幕上。
您可以使用 python switch case 并配置所有 return 类型 attribute name
然后调用该函数,如果它与特定的 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)
更新:使用 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)
我正在练习使用 selenium 登录网站 并获取抵押票据(print/save/download pdf 格式)。
登录页面如下:
字段1:合同号
字段 2:国家用户 ID
Botton:用于验证合约的Botton
字段 3:每次页面 accessed/refreshed
当我在整个用户页面(每个月)登录它时,当涉及到一些我不记得的偶然信息(例如国家健康 ID 或选民登记)时,我刷新页面直到它给我带来了一些容易记住的信息(phone 号码、邮政编码等)。
我该怎么做才能通过最后一个以随机方式请求个人信息的字段(有些我知道,有些我不知道)?
第三个字段的网站代码如下。参数 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()
添加了未检测到的 Selenium 第一个建议的结果
要将 字符序列 发送到 <input>
元素,您需要引入 WebDriverWait for the
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#aleatoryField[formcontrolname='aleatoryField']"))).send_keys("234567")
使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='aleatoryField' and @formcontrolname='aleatoryField']"))).send_keys("234567")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
您似乎对动态元素没有问题,因为元素 id
从未更改过。您遇到基于字段类型的输入值问题 name
出现在每次登录的屏幕上。
您可以使用 python switch case 并配置所有 return 类型 attribute name
然后调用该函数,如果它与特定的 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)
更新:使用 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)