使用ChromeDriver对硒测试案例的超时折磨

发布于 2025-02-04 11:18:32 字数 4911 浏览 3 评论 0原文

我有一个硒测试用例,当无头选项为false时可行,但是当无头选项为true时失败。

环境:

  1. Mac Apple M1 Monterey版本12.2.1
  2. Chromedriver 102.0.5005.61 (0E59BCC00CC4985CE39AD31C150065F159D95AD3-REFS/BRANCH-HEADS/5005@{#819})
  3. Chrome Browser版本102.0.5005.61(官方构建)(官方构建)(ARM64)
import os
import time
from os import path
from os.path import expanduser
from os.path import exists
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

class MSetUp:
    
    def __init__ (self, driverName = 'chromedriver', headless=True):
        if driverName == 'chromedriver':
            path = expanduser("~/")
            if headless:
                chrome_options = Options()
                chrome_options.add_argument("--headless")
                chrome_options.add_argument('--no-sandbox')
                chrome_options.add_argument('--disable-dev-shm-usage')
            else:
                chrome_options = Options()
                chrome_options.add_argument('--no-sandbox')
                chrome_options.add_argument('--disable-dev-shm-usage')
                self.driver = webdriver.Chrome(path + 'chromedriver', chrome_options=chrome_options)
    
    def setupPPMOP(
            self, 
            redirectUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-8E9818988A127210Y", 
            payPalUserEmail = "[email protected]",
            payPalUserPassword = "rteMyPaypal123", 
            payPalUserCountry = 'us'):
        wait = WebDriverWait(self.driver, 30)
        """ Go to paypal url """
        self.driver.get(redirectUrl)

        """ look for consent button to accept cookie """
        time.sleep(5)
        cookieAcceptConcentButton = wait.until(ec.visibility_of_element_located((By.ID, "acceptAllButton")))
        if cookieAcceptConcentButton != None:
            cookieAcceptConcentButton.click()

        """ Find email field and enter paypal user email. """
        emailField = wait.until(ec.visibility_of_element_located((By.ID, "email")))
        emailField.send_keys(payPalUserEmail)
        
        """ Find next button and click """
        nextButton = self.driver.find_element_by_id('btnNext')
        nextButton.click()
        
        """ Find password field and enter paypal user password. """
        passwordField = wait.until(ec.visibility_of_element_located((By.ID, "password")))
        passwordField.send_keys(payPalUserPassword)

        """ Find login button and click """
        loginButton = wait.until(ec.visibility_of_element_located((By.ID, "btnLogin")))
        loginButton.click()
        
        """ find button Agree & Continue and click """
        xpath = '//*[@id="consentButton"]'
        print("xpath = " + xpath)
        agreementButton = wait.until(ec.element_to_be_clickable((By.XPATH, xpath)))

The last step fails with the following exception in headless mode.
>       raise TimeoutException(message, screen, stacktrace)
E       selenium.common.exceptions.TimeoutException: Message:
E       Stacktrace:
E       0   chromedriver                        0x0000000103161170 chromedriver + 4477296
E       1   chromedriver                        0x00000001030f8c18 chromedriver + 4049944
E       2   chromedriver                        0x0000000102d4ad40 chromedriver + 191808
E       3   chromedriver                        0x0000000102d79b28 chromedriver + 383784
E       4   chromedriver                        0x0000000102da285c chromedriver + 551004
E       5   chromedriver                        0x0000000102d6ef58 chromedriver + 339800
E       6   chromedriver                        0x0000000103136bd4 chromedriver + 4303828
E       7   chromedriver                        0x000000010313b094 chromedriver + 4321428
E       8   chromedriver                        0x000000010313f600 chromedriver + 4339200
E       9   chromedriver                        0x000000010313baf4 chromedriver + 4324084
E       10  chromedriver                        0x000000010311a52c chromedriver + 4187436
E       11  chromedriver                        0x0000000103153488 chromedriver + 4420744
E       12  chromedriver                        0x00000001031535ec chromedriver + 4421100
E       13  chromedriver                        0x000000010316792c chromedriver + 4503852
E       14  libsystem_pthread.dylib             0x00000001c4f09240 _pthread_start + 148
E       15  libsystem_pthread.dylib             0x00000001c4f04024 thread_start + 8

I have a Selenium test case that works when headless option is False, but fails when headless option is True.

Environment:

  1. Mac Apple M1 Monterey Version 12.2.1
  2. ChromeDriver 102.0.5005.61
    (0e59bcc00cc4985ce39ad31c150065f159d95ad3-refs/branch-heads/5005@{#819})
  3. Chrome Browser Version 102.0.5005.61 (Official Build) (arm64)
import os
import time
from os import path
from os.path import expanduser
from os.path import exists
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

class MSetUp:
    
    def __init__ (self, driverName = 'chromedriver', headless=True):
        if driverName == 'chromedriver':
            path = expanduser("~/")
            if headless:
                chrome_options = Options()
                chrome_options.add_argument("--headless")
                chrome_options.add_argument('--no-sandbox')
                chrome_options.add_argument('--disable-dev-shm-usage')
            else:
                chrome_options = Options()
                chrome_options.add_argument('--no-sandbox')
                chrome_options.add_argument('--disable-dev-shm-usage')
                self.driver = webdriver.Chrome(path + 'chromedriver', chrome_options=chrome_options)
    
    def setupPPMOP(
            self, 
            redirectUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-8E9818988A127210Y", 
            payPalUserEmail = "[email protected]",
            payPalUserPassword = "rteMyPaypal123", 
            payPalUserCountry = 'us'):
        wait = WebDriverWait(self.driver, 30)
        """ Go to paypal url """
        self.driver.get(redirectUrl)

        """ look for consent button to accept cookie """
        time.sleep(5)
        cookieAcceptConcentButton = wait.until(ec.visibility_of_element_located((By.ID, "acceptAllButton")))
        if cookieAcceptConcentButton != None:
            cookieAcceptConcentButton.click()

        """ Find email field and enter paypal user email. """
        emailField = wait.until(ec.visibility_of_element_located((By.ID, "email")))
        emailField.send_keys(payPalUserEmail)
        
        """ Find next button and click """
        nextButton = self.driver.find_element_by_id('btnNext')
        nextButton.click()
        
        """ Find password field and enter paypal user password. """
        passwordField = wait.until(ec.visibility_of_element_located((By.ID, "password")))
        passwordField.send_keys(payPalUserPassword)

        """ Find login button and click """
        loginButton = wait.until(ec.visibility_of_element_located((By.ID, "btnLogin")))
        loginButton.click()
        
        """ find button Agree & Continue and click """
        xpath = '//*[@id="consentButton"]'
        print("xpath = " + xpath)
        agreementButton = wait.until(ec.element_to_be_clickable((By.XPATH, xpath)))

The last step fails with the following exception in headless mode.
>       raise TimeoutException(message, screen, stacktrace)
E       selenium.common.exceptions.TimeoutException: Message:
E       Stacktrace:
E       0   chromedriver                        0x0000000103161170 chromedriver + 4477296
E       1   chromedriver                        0x00000001030f8c18 chromedriver + 4049944
E       2   chromedriver                        0x0000000102d4ad40 chromedriver + 191808
E       3   chromedriver                        0x0000000102d79b28 chromedriver + 383784
E       4   chromedriver                        0x0000000102da285c chromedriver + 551004
E       5   chromedriver                        0x0000000102d6ef58 chromedriver + 339800
E       6   chromedriver                        0x0000000103136bd4 chromedriver + 4303828
E       7   chromedriver                        0x000000010313b094 chromedriver + 4321428
E       8   chromedriver                        0x000000010313f600 chromedriver + 4339200
E       9   chromedriver                        0x000000010313baf4 chromedriver + 4324084
E       10  chromedriver                        0x000000010311a52c chromedriver + 4187436
E       11  chromedriver                        0x0000000103153488 chromedriver + 4420744
E       12  chromedriver                        0x00000001031535ec chromedriver + 4421100
E       13  chromedriver                        0x000000010316792c chromedriver + 4503852
E       14  libsystem_pthread.dylib             0x00000001c4f09240 _pthread_start + 148
E       15  libsystem_pthread.dylib             0x00000001c4f04024 thread_start + 8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文