【问题标题】:Python Selenium - slow send_keys() execution on LinuxPython Selenium - Linux 上的缓慢 send_keys() 执行
【发布时间】:2021-05-27 01:43:30
【问题描述】:

我的 Selenium 测试遇到了最奇怪的问题。 我有以下课程:

class DataStorageTestSuiteChrome(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome(executable_path="chrome_driver_path")
        self.driver.maximize_window()
        self.driver.get("www.data_storage_website.com")
        self.page = DataStoragePage(self.driver)

    def tearDown(self):
        self.driver.quit()

    def test_sn_validation(self):
        self.page.sn_element = 20001234567    # This line takes 2.5min to execute in Linux
        self.assertFalse(self.page.is_valid_sn())

        self.page.sn_element = self.ID_GENERATOR.sn
        self.assertTrue(self.page.is_valid_sn())
class DataStoragePage:
    '''Page Object for DataStorage page'''
    sn_element = SnElement()

    def __init__(self, driver):
        self.driver = driver
class SnElement:
    '''This class gets the search text from the specified locator'''
    locator = "some_working_locator"

    def __set__(self, obj, value):
        """Sets the text to the value supplied"""
        driver = obj.driver
        WebDriverWait(driver, 100).until(
            lambda driver: driver.find_element(By.XPATH, self.locator))
        driver.find_element(By.XPATH, self.locator).clear()
        driver.find_element(By.XPATH, self.locator).send_keys(value)   # This line takes 2.5min to execute in Linux

现在,问题是测试用例“test_sn_validation”在 Windows 中执行需要几秒钟(都开启和关闭无头模式),而在 WSL2 Ubuntu 中需要 2.5 分钟。这显然让我很困惑,因为我希望这样的测试在 Linux 上运行得更快。

由于 send_keys() 方法,我已经跟踪到 SnElement 类中的 driver.find_element(By.XPATH, self.locator).send_keys(value) 行为缓慢的原因。此外,这种延迟只发生在第一次运行 send_keys() 时——我通过 PDB 运行的每次后续执行都是瞬时的。

有没有人在 WSL2 中看到 send_keys() 的类似行为,并且可以推荐解决方案?

【问题讨论】:

    标签: python selenium selenium-webdriver xpath


    【解决方案1】:

    我认为它与 Linux 无关。 两个可能的原因:

    1 您的some_working_locator 无法正常工作。但是,我不知道为什么如果在 150 秒后无法定位,而不是在 150 秒后。

    2您的等待不正确。合适的方式是:

    WebDriverWait(driver, 100).until(
                    lambda driver: driver.find_element_by_xpath(self.locator))
    

    注意,定位器应该是字符串。

    3尝试使用:

    wait.until(EC.element_to_be_clickable((By.XPATH, 'self.locator')))
    

    wait.until(EC.visibility_of_element_located((By.XPATH, 'self.locator'))) 
    

    相反。检查这个答案https://stackoverflow.com/a/41873287/12730112

    您的项目中也可能有隐式等待,这可能是等待时间更长的原因。不要设置等待那么久。对于 99% 的情况,30 秒就足够了。

    4 还有一点说明: 将 SnElement 的最后一行更改为

    driver.find_element_by_xpath(self.locator).clear()
    driver.find_element_by_xpath(self.locator).send_keys(value)
    

    并检查它是否有帮助。

    【讨论】:

    • 不幸的是,事实并非如此。从字面上看,它是 send_keys() 方法,它在第一次运行时将代码冻结 2.5 分钟(在 PDB 中检查)。
    • 1.首先尝试用上面建议的替换这个等待。 2 你在使用隐式等待吗? 3 定位器是什么?
    【解决方案2】:

    简而言之,它的“打字”速度很慢,有一种从剪贴板粘贴的解决方法。至于配置打字速度,不能多说。

    解决方法如下:

    import pyperclip
    from selenium.webdriver.common.keys import Keys
    pyperclip.copy('foo')
    element.send_keys(Keys.CONTROL, 'v')
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-17
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 2021-08-02
      • 2018-06-18
      相关资源
      最近更新 更多