Automating A Workflow Using Selenium - Best Way To Fetch Elements & Make Code More Generic












0












$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")









share|improve this question











$endgroup$





This question has an open bounty worth +50
reputation from Dhiwakar Ravikumar ending in 7 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
    yesterday
















0












$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")









share|improve this question











$endgroup$





This question has an open bounty worth +50
reputation from Dhiwakar Ravikumar ending in 7 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
    yesterday














0












0








0





$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")









share|improve this question











$endgroup$




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






python interview-questions selenium






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 mins ago







Dhiwakar Ravikumar

















asked 2 days ago









Dhiwakar RavikumarDhiwakar Ravikumar

10316




10316






This question has an open bounty worth +50
reputation from Dhiwakar Ravikumar ending in 7 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.








This question has an open bounty worth +50
reputation from Dhiwakar Ravikumar ending in 7 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
    yesterday


















  • $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
    yesterday
















$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
yesterday




$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
yesterday










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213234%2fautomating-a-workflow-using-selenium-best-way-to-fetch-elements-make-code-mo%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213234%2fautomating-a-workflow-using-selenium-best-way-to-fetch-elements-make-code-mo%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga