Seleniumbase 结账机器人脚本运行速度不够快

问题描述 投票:0回答:1

我需要帮助让这个用 seleniumbase 编写的结账机器人脚本运行得更快。我希望它能购买发布到网站上的任何新商品,而网站上的商品在几秒钟内就会售罄,这基本上意味着有一个机器人比我的更快,因为我一直在被击败。我让它每秒刷新几次页面以查找新发布的项目。也许有比检查主页上新的 href 链接而不是写入文件更好的方法?或者也许 seleniumbase 不是最快的方法?

from seleniumbase import BaseCase
import time

class MyTestClass(BaseCase):
    def save_urls_to_file(self, url_list, file_path):
        with open(file_path, 'a') as file:  # Use 'a' (append) mode instead of 'w' (write) mode
            for url in url_list:
                file.write(url + '\n')

    def read_urls_from_file(self, file_path):
        with open(file_path, 'r') as file:
            return [line.strip() for line in file]

    def get_all_href_links(self):
        elements = self.find_elements('a')
        return [element.get_attribute('href') for element in elements if element.get_attribute('href')]

    def search_for_new_urls(self, main_url, file_path, processed_file_path):
        self.open(main_url)
        all_urls = self.get_all_href_links()

        # Filter out already processed URLs
        processed_urls = self.read_urls_from_file(processed_file_path)
        new_urls = list(set(all_urls) - set(processed_urls))

        if new_urls:
            self.save_urls_to_file(new_urls, file_path)
            with open(processed_file_path, 'a') as processed_file:
                for new_url in new_urls:
                    processed_file.write(new_url + '\n')

            for new_url in new_urls:
                self.run_custom_code(new_url)

    def run_custom_code(self, url):
        self.open(url)
        self.click("div.sqs-add-to-cart-button-inner")
        self.click('div[data-test="continue-to-cart"]')
        self.click('button[data-test="cart-button"]')
        self.type('input[name="email"]', "[email protected]", timeout=2)
        self.click('button[data-test="continue-button"]')
        self.type('input[name="fname"]', "John")
        self.type('input[name="lname"]', "Doe")
        self.type('input[data-test="line1"]', "123 Easy")
        self.click('span:contains("Street, Any City, FL, USA")'
        time.sleep(.5)
        self.click('button[data-test="continue-button"]')
        self.click('button[data-test="continue-button"]')
        self.switch_to_frame('//iframe[contains(@name, "privateStripeFrame")]', timeout=2)
        self.find_element('input[name="cardnumber"]').send_keys('4242 4242 4242 4242')
        self.find_element('input[name="exp-date"]').send_keys('00 01')
        self.find_element('input[name="cvc"]').send_keys('346')
        self.switch_to_default_content()
        self.click('button[data-test="continue-button"]')
        self.click('button[data-test="purchase-button"]')        
        time.sleep(2)
        self.save_screenshot("new.png")

    def process_new_urls(self, main_url, file_path, processed_file_path):
        while True:
            self.search_for_new_urls(main_url, file_path, processed_file_path)
            time.sleep(.2)  

    def test_run_script(self):
        main_url = "http://example.com/shop"  
        file_path = "/home/urls.txt"  
        processed_file_path = "/home/processed_urls.txt"  

        self.process_new_urls(main_url, file_path, processed_file_path)

if __name__ == "__main__":
    MyTestClass().test_run_script()

我尝试删除

time.sleep()
,但
.5
需要位于其中,否则就会挂起。我也尝试等待元素加载,但这不起作用。这只是网站的限制。

python selenium-webdriver bots checkout seleniumbase
1个回答
0
投票

使用 SeleniumBase,您可以通过一些方法来加快测试速度。

以下是您应该了解的一些 pytest 命令行选项:

  • --pls=none
    --> 将
    pageLoadStrategy
    设置为
    "none"
    :此策略使 Selenium 在浏览器完全接收到初始 HTML 内容后立即返回。

  • --sjw
    --> 跳过JS等待,如
    wait_for_ready_state_complete()

这些可能会有所帮助,但如果您使用混合的原始硒命令,可能会导致测试不稳定。

要消除加速过程中可能出现的不稳定现象,请像这样交换行:

self.find_element('input[name="cvc"]').send_keys('346')

用这个代替:

self.type('input[name="cvc"]', '346')

但是,您可能还会发现使用内置的 JS 方法要快一些。所以不要使用这个:

self.type(selector, text)

你可以用这个代替:

self.js_type(selector, text)

还有一个

js_click()
方法:

self.js_click(selector)

还有

--headless
模式,可以加快测试速度:

pytest --headless

合并所有这些更改后,如果操作正确,您的脚本应该会得到很好的速度提升。

© www.soinside.com 2019 - 2024. All rights reserved.