Password Change Template - Selenium [AWS Console]

:no_entry: WARNING

Prerequisites: Selenium IDE, Browser Element Inspection

All bracketed terms with generic names are placeholders to be replaced with specific information.


Performing Password Change with Selenium

Utilize the Selenium application, in this article, we use the Chrome extension Selenium IDE.

1. Access the application and click on “Record a new test in a new project.”

2. Afterward, provide an arbitrary name for the project and click “Ok.”

The base URL is the authentication URL for the website, in this case:

https://[ACCOUNT_ID].signin.aws.amazon.com/console.

3. Click to start recording, and a new tab will open with a prompt indicating that Selenium IDE is recording, as shown below:

image

4. Authenticate in the solution and perform the password change with the IDE recording. Access the menu in the AWS Console: Authenticate → User → Security Credentials → Update Console Password .

5. Complete the entire recording, close the IDE browser, and review the information in the application, as shown below:

image

We will use the information from the elements that were clicked during the password change.


Most Used Selenium Commands Explained

ATTENTION

During the template creation, it is necessary to locate the elements we need to interact with, such as input fields and buttons. To locate these elements, we primarily use Xpath (the full path of the element) or Name (information related to the element’s name) parameters. The most reliable parameter is Xpath, but there is a specific moment where it is not possible to use it, which will be described shortly.

  • Comments - Used to receive feedback on the password change execution:
print("[comment]")
  • Accessing the URL - Used to access a URL, mainly at the beginning of the template and when we need to navigate to a new URL (e.g., a password change screen):
self.driver.get("[target_url]")
  • Waiting for an element’s presence - Important when a new tab or screen is opened to allow time for the elements to load before executing any actions:
WebDriverWait(self.driver, [time_seconds]).until(expected_conditions.presence_of_element_located((By.XPATH, '[xpath_element]')))
  • Waiting for an element to disappear - Has the same purpose as the previous command but, unlike waiting for an element to appear, this command waits for it to disappear:
WebDriverWait(self.driver, 10).until(expected_conditions.invisibility_of_element_located((By.XPATH, '//*[@id="signin_button"]')))
  • Locate Element and Fill - to locate the element that needs to be filled, such as the username field, and then the “send_keys” command automatically fills it:
self.driver.find_element(By.XPATH, '[xpath_element]').send_keys("[text]")
  • Locate Element and Fill with the Name parameter:
self.driver.find_element(By.NAME, "[name_element]").send_keys("[text]")
  • Locate Element and Click - Used to click on buttons:
self.driver.find_element(By.XPATH, '[xpath_element]').click()

Most commonly used TAGs in template creation:

[#HOSTNAME#]: Device hostname
[username#]: Credential username
[#NEW_PASSWORD#]: Value of the new password
[#CURRENT_PASSWORD#]: Value of the current password


Template Creation

1. To create the password change template using the Selenium protocol, access the web menu at Executions → Settings → Template → New and follow the settings below:

It is recommended to perform one step at a time, test the password change, and check the execution process in the logs through comments.


Authentication

1. it is necessary to open the link that leads to the authentication screen:

self.driver.get("https://[ACCOUNT_ID].signin.aws.amazon.com/console")

2. To authenticate, you need to fill in the username and password fields and then click the Login button. Therefore, it is necessary to copy the XPath of these 3 elements. To make this easier, you can copy it from the IDE by selecting the desired element and checking the “Target,” as shown below:

image

3. Consequently, the XPath for the username field is //*[@id=“username”]. Adapting the commands provided in section 2, we first need to wait for the page to load, waiting for the username field to appear:

WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="username"]')))
self.driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("[#USERNAME#]")
self.driver.find_element(By.XPATH, '//*[@id="password"]').send_keys("[#CURRENT_PASSWORD#]")

4. For the Login button’s XPath, we have the following: xpath=//div[@id=‘input_signin_button’]/a. When Selenium provides the XPath with div or /a, you can replace the div with * and ignore the /a, leaving only: //[@id=‘input_signin_button’] for consistency. Therefore, to authenticate by clicking the button:

self.driver.find_element(By.XPATH, '//*[@id="signin_button"]').click()

Password Change Screen

1. In the case of AWS, to change the password, we need to click on the user and go to “Security Credentials”.

image

2. This case illustrates a common issue related to web execution, which is floating menus. It is not possible to collect an element identifier, so it is better to use the command to open a new URL. Therefore, copy the URL that appears when you click the Credentials button. Opening the new URL:

self.driver.get("https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-east-1#/security_credentials?section=IAM_credentials")

In the case of the “Update Console Password” button, we encounter the following issue with pure XPath: it changes every time the page is refreshed. This situation can be noticed whenever there are sequential numbers in the identifier. For example, when copying the XPath for the button: //*[@id=“awsui-tabs-0-8102-IAM_credentials-panel”].

To click on the password change field, we need to use a different way for Selenium to locate this element. We can use “contains,” a way to find the element based on its visual content. Following this logic, the XPath looks like this: //button[contains(.,‘Update console password’)].

Therefore, waiting for the element’s presence and clicking:

WebDriverWait(self.driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "//button[contains(.,'Update console password')]")))
self.driver.find_element(By.XPATH, "//button[contains(.,'Update console password')]").click()

Performing Password Change

To perform the password change, simply fill in the following information and click Confirm:

image

Since these fields are in a floating screen, the XPath selector also changes when the page is refreshed. Therefore, we will locate the elements based on the Name identifier. In the case of filling in the current password, we have the following name:

image

So, filling in all the password fields:

self.driver.find_element(By.NAME, "current").send_keys("[#CURRENT_PASSWORD#]")
self.driver.find_element(By.NAME, "new").send_keys("[#NEW_PASSWORD#]")
self.driver.find_element(By.NAME, "confirm").send_keys("[#NEW_PASSWORD#]")

To complete the template, click to confirm:

self.driver.find_element(By.XPATH, "//*[contains(.,'Change password')]").click()

The next section will provide the complete template for copying, including all comments and the password change result, demonstrating the importance of using the print command.


Template Ready for Copying

# Open login URL
print("Open login URL")
self.driver.get("https://[ACCOUNT_ID].signin.aws.amazon.com/console")

# Waiting for username field
print("Waiting for username field")
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="username"]')))

#Filling Username and Password
print("Filling Username and Password")
self.driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("[#USERNAME#]")
self.driver.find_element(By.XPATH, '//*[@id="password"]').send_keys("[#CURRENT_PASSWORD#]")

#Authentication
self.driver.find_element(By.XPATH, '//*[@id="signin_button"]').click()
WebDriverWait(self.driver, 10).until(expected_conditions.invisibility_of_element_located((By.XPATH, '//*[@id="signin_button"]')))
print("Authentication with success")

#Open Password Change URL
self.driver.get("https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-east-1#/security_credentials?section=IAM_credentials")
print("Open Password Change URL")
print("Waiting for password change field")
WebDriverWait(self.driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "//button[contains(.,'Update console password')]")))
print("Found the Password Change Field")

#Click on Password Change Field
self.driver.find_element(By.XPATH, "//button[contains(.,'Update console password')]").click()
#WebDriverWait(self.driver, 10).until(expected_conditions.invisibility_of_element_located((By.XPATH, "//button[contains(.,'Update console password')]")))
print("Clicked on Rotate Password")

#Filling the Password Changes Informations
#WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//button[contains(.,'Change Password')]")))
print("Screen for password rotation")
self.driver.find_element(By.NAME, "current").send_keys("[#CURRENT_PASSWORD#]")
self.driver.find_element(By.NAME, "new").send_keys("[#NEW_PASSWORD#]")
self.driver.find_element(By.NAME, "confirm").send_keys("[#NEW_PASSWORD#]")
print("Filled All Information")

#Confirm the Password Rotation
self.driver.find_element(By.XPATH, "//span[contains(.,'Change password')]").click()
print("It worked")

Execution Result: