1

I am practicing with selenium to log into a website and grab the mortage ticket (print/save/download it as pdf).

The logging page is as follows:

Field 1: Contract number

Field 2: National user-id

Botton: Botton to validate the contract

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

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.).

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)?

The website code referring to the 3rd field is as follows. The parameters name="zipCode" and placeholder="Zip Code" parts are the ones that keeps changing their values each time the page is refreshed.

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 result

  • could you please post the html of other elements as well `day of birh, mother's name, National Health ID, voter registration ` to know what's the similarity of each dynamic elements. – KunduK Apr 08 '22 at 15:45
  • Thank you KunduK. I just added some more html. – theflteacher Apr 08 '22 at 16:41
  • it seems in the solution the element locator provided is correct for all input fields. I think your problem the input value that you are looking for right. If postcode input then postcode should enter or it is voterid then voterid should enter. Am I right? – KunduK Apr 08 '22 at 17:05

2 Answers2

1

It seems you have no problem with dynamic element since element id was never changed. you have problem with input values based on type of the field name appeared on the screen on each login.

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.

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

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)
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • I got this error when running the entire code ``` File "boleto.py", line 32 match elementAttVal: ^ SyntaxError: invalid syntax # line 32 refers to: match elementAttVal ``` – theflteacher Apr 08 '22 at 21:55
  • Are you able to print the attribute value what’s it printing? print(elementAttVal) – KunduK Apr 08 '22 at 22:02
  • switch case support python 3.10 and above. You could try the updated answer as well. hope that resolve your issue.let me know how updated one goes. – KunduK Apr 08 '22 at 22:14
  • 1
    It just works. Thank you KunduK!!! It was my first project-to-use-in-real-world. I had to access the site through the homepage each month to grab the ticket. Now it is piece of cake! Also, I've never used a function without a return command (I am a super noob with Python). I've learned a bunch of things today. – theflteacher Apr 09 '22 at 00:20
0

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:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#aleatoryField[formcontrolname='aleatoryField']"))).send_keys("234567")
    
  • Using 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • ndetected Selenium, thank you! I don't get if your suggestions are for only one try. Because it only runs once. Most of times, the key does not match the current name="value" prompt. I've inserted a picture of the result. I filled the key text with the due day for payment (12). And in the example, the random field was the day of birth. Also, I don't know if it does make any difference. I added a final accesss botton do my code. I had forgotten in the first time. – theflteacher Apr 08 '22 at 15:02
  • _it only runs once. Most of times, the key does not match the current name="value" prompt_: Can you elaborate more about your usecase? – undetected Selenium Apr 08 '22 at 18:46
  • It just happens that the two first fields are static. It requests the same values all the time. After these values are validated, the third one appears. Each time you access the page, the third field requests different aleatory information (day of birth, year of birth, voter registration, phone number...). I would like to address these random possibilities. Since if I just put ONE single value for this field, say "1990", it hardly will match with the random possibility, like "phoneNumber", or "dayBirth", etc. I've inserted more html in my question above. – theflteacher Apr 08 '22 at 22:09