How to make selenium in python faster 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.
#startup
chrome_options = Options()
chrome_options.add_argument(&quot;--headless&quot;)
driver = webdriver.Chrome(options=chrome_options)
driver = webdriver.Chrome('chromedriver')
driver.maximize_window()
driver.get(&quot;https://www.games.rs/ps4-konzole/16794-konzola-playstation-4-1tb-pro-black-dualshock-fifa-21&quot;)


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 &quot;Add to Cart&quot;.
#order
start_time = time.time() 
kupi = driver.find_element_by_xpath('//*[@class=&quot;block product-attributes-wrapper clearfix&quot;]/ul[1]/li[1]/div[1]/div[1]')
cena = driver.find_element_by_xpath('//*[@class=&quot;block product-attributes-wrapper clearfix&quot;]/ul[1]/li[1]/div[1]/div[1]')
driver.find_element_by_xpath('//*[@id=&quot;modal-cookie-info&quot;]/div/div/div/div/button').click()
driver.execute_script(&quot;window.scrollTo(712, 200);&quot;, kupi)
while True:
    try:
        driver.find_element_by_xpath('//button[text()=&quot;Dodaj u korpu&quot;]')
    except NoSuchElementException:
        driver.refresh()
    else:
        driver.find_element_by_xpath('//button[text()=&quot;Dodaj u korpu&quot;]').click()
        break

driver.find_element_by_xpath('//*[@id=&quot;miniCartContent&quot;]/div[1]/a').click()
driver.execute_script(&quot;window.scrollTo(0 , 900);&quot;, )

Lastly login for informations and completing order.
#login
driver.find_element_by_xpath('//*[@id=&quot;order_address_content&quot;]/div/div/div[2]/div[2]/div[1]/div[1]/div[2]/div/div/a').click()
time.sleep(0.2)
email = driver.find_element_by_css_selector('#login_email')
email.send_keys(&quot;mail&quot;)

password = driver.find_element_by_id('login_password')
password.send_keys(&quot;pass&quot;)
driver.find_element_by_xpath('//*[@id=&quot;login_modal&quot;]/div/div/form/div[3]/button').click()
time.sleep(0.2)
driver.execute_script(&quot;window.scrollTo(0 , 1600);&quot;, )
driver.find_element_by_xpath('//*[@class=&quot;delivery-options list-unstyled&quot;]/li[3]/div[1]/div[1]').click()
time.sleep(0.5)
driver.execute_script(&quot;window.scrollTo(0 , 2500);&quot;, )
driver.find_element_by_xpath('//*[@id=&quot;submit_order_one_page&quot;]').click()

This prints duration of the program.
print(&quot;--- %s seconds ---&quot; % (time.time() - start_time))
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, &quot;//button[text()=&quot;Dodaj u korpu&quot;]&quot;)))
)

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.