1
\$\begingroup\$

I'm a newbie to Selenium and I'm trying to get better at it by taking up interview assignments. This YouTube link describes the workflow that needs to be automated.

The following points are worth taking into account.

  1. For your purpose, use your own gmail id/password but make it readable from a file in your project.

  2. Use the fashion photo pasted below as input.

    enter image description here

I implemented the following solution and I'd like to know if there's any way to improve it. I'm interested in any changes that can be made, to make the script more robust in addition to any new concepts or tools in Selenium or Python that might make life easier as well as existing concepts and tools that I could implement a whole lot better.

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

try:
    # STEP 1 - LOAD PAGE
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    chrome_browser = webdriver.Chrome(chrome_options=options)
    chrome_browser.get("https://huew.co")
    WebDriverWait(chrome_browser,10).until(EC.title_contains("Huew | Your Catalog of Shoppable Fashion Inspirations"))

    # STEP 2 - CLICK ON "YOU"
    you_button = chrome_browser.find_element_by_xpath('//div[@class="desktop-menu-icon-text" and text()="YOU"]')
    time.sleep(2)
    you_button.click()

    # STEP 3 - CLICK ON "LOGIN USING GOOGLE"
    google_login_button = chrome_browser.find_element_by_xpath('//img[@class="social-login-button" and @ng-click="login(\'google\')"]')
    google_login_button.click()

    # STEP 4 - READ USERNAME AND PASSWORD FOR THE GOOGLE ACCOUNT
    with open("credentials.txt", "r") as f:
        lines = f.readlines()
        for line in lines:
            username, password = line.split(":")

    # STEP 5 - SWITCH TO THE NEW WINDOW
    window_handles = chrome_browser.window_handles
    parent_handle = chrome_browser.current_window_handle
    for window_handle in window_handles:
        if window_handle != parent_handle:
            chrome_browser.switch_to.window(window_handle)
            break

    # STEP 6 - TYPE IN USERNAME AND PASSWORD
    input_field = WebDriverWait(chrome_browser, 5).until(EC.visibility_of_element_located((By.ID, "identifierId")))
    input_field.send_keys(username)
    next_button = chrome_browser.find_element_by_xpath('//span[text()="Next"]')
    next_button.click()

    input_field = WebDriverWait(chrome_browser, 5).until(EC.visibility_of_element_located((By.NAME, "password")))
    input_field.send_keys(password)
    next_button = chrome_browser.find_element_by_xpath('//span[text()="Next"]')
    next_button.click()
    time.sleep(5)

    # STEP 7 - SWITCH BACK TO THE OLD WINDOW AND CLICK "DISCOVER"
    chrome_browser.switch_to.window(parent_handle)
    discover_button = chrome_browser.find_element_by_xpath('//div[@class="desktop-menu-icon-text" and text()="DISCOVER"]')
    discover_button.click()

    # STEP 8 - UPLOAD THE PHOTO AND CLICK ON "SUBMIT"
    file_upload_panel = WebDriverWait(chrome_browser, 60).until(lambda chrome_browser: chrome_browser.find_element_by_xpath('//input[@type="file"]'))
    file_upload_panel.send_keys(os.getcwd()+"\\fashion_pic.jpg")
    submit_button = WebDriverWait(chrome_browser, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.desktop-photo-submit.ng-scope")))
    chrome_browser.execute_script('''document.querySelector('button.desktop-photo-submit.ng-scope').click()''')

    # STEP 9 - WAIT FOR THE "SAVE" BUTTON TO APPEAR AND CLICK ON IT
    save_button = WebDriverWait(chrome_browser, 60).until(EC.element_to_be_clickable((By.XPATH, '//button[text()="SAVE"]')))
    save_button.click()
    time.sleep(5)

    # STEP 10 - CLICK ON "HERE" LINK
    here_link = chrome_browser.find_element_by_xpath('//a[text()="here"]')
    here_link.click()

except ValueError:
    print("Please ensure that the input file has the username and password saved in the format specified in readme.txt")
\$\endgroup\$

This question has an open bounty worth +50 reputation from Dhiwakar Ravikumar ending in 4 days.

The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.

I would like an honest feedback of what I've written to accomplish what I've been tasked with. I'd also like any guidance on how to improve the snippet of code that I've written in addition to any links to guides or source material that can help be become a better selenium tester.

  • \$\begingroup\$ The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ – Toby Speight Feb 12 at 9:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.