I'm making a bot for PlayStation 5. And restock is coming soon so I want to make my bot faster. The approximate duration is 4.90 seconds. Is there any way to make this faster? I found that I should write this in C or C ++ for better results.

chrome_options = Options()
driver = webdriver.Chrome(options=chrome_options)
driver = webdriver.Chrome('chromedriver')

Cena and Kupi are unnecessary. Program clicks to accept cookies. Then scrolling cuz it needs to. And then refreshing page until text in button is "Add to Cart".

start_time = time.time() 
kupi = driver.find_element_by_xpath('//*[@class="block product-attributes-wrapper clearfix"]/ul[1]/li[1]/div[1]/div[1]')
cena = driver.find_element_by_xpath('//*[@class="block product-attributes-wrapper clearfix"]/ul[1]/li[1]/div[1]/div[1]')
driver.execute_script("window.scrollTo(712, 200);", kupi)
while True:
        driver.find_element_by_xpath('//button[text()="Dodaj u korpu"]')
    except NoSuchElementException:
        driver.find_element_by_xpath('//button[text()="Dodaj u korpu"]').click()

driver.execute_script("window.scrollTo(0 , 900);", )

Lastly login for informations and completing order.

email = driver.find_element_by_css_selector('#login_email')

password = driver.find_element_by_id('login_password')
driver.execute_script("window.scrollTo(0 , 1600);", )
driver.find_element_by_xpath('//*[@class="delivery-options list-unstyled"]/li[3]/div[1]/div[1]').click()
driver.execute_script("window.scrollTo(0 , 2500);", )

This prints duration of the program.

print("--- %s seconds ---" % (time.time() - start_time))

  • 1
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$
    – BCdotWEB
    Commented Nov 25, 2020 at 21:02

1 Answer 1


Just a short review from me:

The artificial delays (time.sleep(0.5)) are superfluous and not safe. They slow you down sometimes, they fail sometimes, because the pages take longer than usual to load. Instead you can use Selenium selector functions, to wait until an element appears (or disappears). For example, you could have something like this:

element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//button[text()="Dodaj u korpu"]")))

Here, we wait for an element to be in clickable state, but set a timeout of 10 seconds for the condition to be realized. You have a solid range of similar options available. Check out the doc: Waits

Likewise, you can get rid of the exception handling block (NoSuchElementException). My advice is to replace the sleeps with adequate wait functions, depending on the conditions you want to test. This should make your code more reliable and probably a bit faster, because it's not going to wait for longer than necessary.

Since your concern is speed: I would say 4.90 seconds is not bad but I don't know your constraints. Have you benchmarked your code to find out what is taking time ? Use the timeit module or - recommended link: Python Timer Functions: Three Ways to Monitor Your Code to set up your own timing class.

From experience, Selenium takes time to load, so perhaps you might as well leave it running in the background and use a loop to refresh the page at regular intervals, while being gentle and taking care not to overload the site.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.