2
\$\begingroup\$

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("--headless") driver = webdriver.Chrome(options=chrome_options) driver = webdriver.Chrome('chromedriver') driver.maximize_window() driver.get("https://www.games.rs/ps4-konzole/16794-konzola-playstation-4-1tb-pro-black-dualshock-fifa-21") 

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

#order 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.find_element_by_xpath('//*[@id="modal-cookie-info"]/div/div/div/div/button').click() driver.execute_script("window.scrollTo(712, 200);", kupi) while True: try: driver.find_element_by_xpath('//button[text()="Dodaj u korpu"]') except NoSuchElementException: driver.refresh() else: driver.find_element_by_xpath('//button[text()="Dodaj u korpu"]').click() break driver.find_element_by_xpath('//*[@id="miniCartContent"]/div[1]/a').click() driver.execute_script("window.scrollTo(0 , 900);", ) 

Lastly login for informations and completing order.

#login driver.find_element_by_xpath('//*[@id="order_address_content"]/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("mail") password = driver.find_element_by_id('login_password') password.send_keys("pass") driver.find_element_by_xpath('//*[@id="login_modal"]/div/div/form/div[3]/button').click() time.sleep(0.2) driver.execute_script("window.scrollTo(0 , 1600);", ) driver.find_element_by_xpath('//*[@class="delivery-options list-unstyled"]/li[3]/div[1]/div[1]').click() time.sleep(0.5) driver.execute_script("window.scrollTo(0 , 2500);", ) driver.find_element_by_xpath('//*[@id="submit_order_one_page"]').click() 

This prints duration of the program.

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

\$\endgroup\$
1
  • 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
    CommentedNov 25, 2020 at 21:02

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.