2

I wrote this automated bot in PyCharm and I do not know why my code executes so slow? Any help for improvement is sincerely appreciated.

from selenium import webdriver
from InfoApp import keys
import time
def order(k):
    driver = webdriver.Chrome(executable_path='driver/chromedriver')
    driver.get(k['product_url'])
    driver.maximize_window()
    driver.find_element_by_xpath('//*[@id="RightRail"]/div/div[4]/fieldset/div/div[7]/label').click()
    driver.execute_script("window.scrollTo(0, 1000)")
    driver.find_element_by_xpath('//*[@id="floating-atc-wrapper"]/div/button').click()
    driver.find_element_by_xpath('//*[@id="PDP"]/div/div[4]/div/div/div/div/div/div/div/div/div/div[3]/div/button[2]').click()
    time.sleep(5)
    driver.find_element_by_xpath('//*[@id="nav-cart"]/a/div/span').click()
    driver.find_element_by_xpath('//*[@id="react-root"]/div/div[4]/button').click()
    driver.find_element_by_xpath('//*[@id="qa-guest-checkout"]').click()
    driver.find_element_by_xpath('//*[@id="firstName"]').send_keys(k["First_Name"])

if __name__ == '__main__':
    order(keys)
3
  • It has unnecessary time.sleep which can be changed to use webdriver waits. Commented Feb 19, 2021 at 22:25
  • I added the time.sleep because the page was not fully loaded and I have a line of syntax to scroll down the page and click, it was not finding the button as the page was not fully loaded so I added the time.sleep. So instead of time.sleep i should use driver.implicitly_wait(5)? Commented Feb 19, 2021 at 22:45
  • Pretty much time sleep is never used in automation Commented Feb 20, 2021 at 0:23

1 Answer 1

1

remove time.sleep and add explicit or implicit wait:

you can add implicit wait as

driver = webdriver.Chrome(executable_path='driver/chromedriver')
driver.implicitly_wait(10)

this will automatically make all commands to wait maximum of 10 seconds . If element not found in 10 sec then element not found is thrown.

but this will not check clicability , visibility etc , this checks only for presence of element. For other condition you can use explicit wait.

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

wait = WebDriverWait(browser, 10)

elem= wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'somelocator')))

Note:

There is good video link jeff added but to add to that :

implicit waits are not bad practices, mixing implicit wait and explicit wait is bad practice. As implict wait waits for only the presence of the elment so in most cases you cannot avoid explicit wait as you need to wait for visibility,clicability etc . so its better to avoid it completely as it cause long waits in your code when you mix both implicit and explicit wait together

So if you are using explicit wait DONT use implict wait

Sign up to request clarification or add additional context in comments.

4 Comments

Please don't add a line of hyphens in your answers. It adds a whole sentence of large bolded text.
Implicit waits are a bad practice according to several Selenium contributors including Simon Stewart, Selenium project lead. See his seleniumconf talk: youtu.be/gyfUpOysIF8?t=2200. Explicit waits should be used instead.
@JeffC implicit waits are not bad practices, mixing implicit wait and explicit wait is bad practice. As implict wait for only the presence of the elment so its better to avoid it completely as it cause long waits in your code when you mix both together. So as you cannot avoid explicit wait you should not be using implicit wait
No. Watch the video. Simon is the guy that wrote the implicit wait code and he clearly said don't use it... use WebDriverWait instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.