为什么元组解包不适用于 Selenium WebDriverWait?

Why tuples unpacking is not working with Selenium WebDriverWait?

如何使用 Selenium 解压元组 WebDriverWait

为什么这段代码不起作用?

class Test:

    def __init__(self, driver):
        self.driver = driver
        
    editprofile_button = (By.XPATH, "//android.widget.TextView[@text=\"Edit profile info\"]")
    
    def profile_page_should_be_visible(self):
        wait = WebDriverWait(self.driver,20)
        wait.until(EC.presence_of_element_located(*Test.editprofile_button))
        assert self.driver.find_element(*Test.editprofile_button).is_displayed()

但是如果我这样写函数,那么一切都可以使用 WebdriverWait:

def profile_page_should_be_visible(self):
    wait = WebDriverWait(self.driver,20)
    wait.until(EC.presence_of_element_located((By.XPATH, "//android.widget.TextView[@text=\"Edit profile info\"]")))
    assert self.driver.find_element(*Test.editprofile_button).is_displayed()

presence_of_element_located,与大多数其他预期条件一样,将 WebElement 定位在 __call__

class presence_of_element_located(object):
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        return _find_element(driver, self.locator)

其中 self.locator 是元组。

def _find_element(driver, by):
    try:
        return driver.find_element(*by)
    except NoSuchElementException as e:
        raise e

_find_element 使用 driver.find_element(*by) 其中 by 是元组。如您所见,元组已在内部解包。

无需解压即可使用Test.editprofile_button

wait.until(EC.presence_of_element_located(Test.editprofile_button))