片段
EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, name.split(' ', 1)[0])) and
EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, name.split(' ', 1)[1]))
只是评估为
EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, name.split(' ', 1)[1]))
所以它总是只检查那个条件和那个条件,即它总是只试图找到Doe
,完全忽略John
。这就是为什么你发现Jane Doe
它之前出现过。
这不是您检查多个条件的方式,您需要将一个类似函数的对象传递给.until
,它可以检查多个条件并返回一个真/假值。
对于您的特定需求,该功能可能看起来像 -
def presence_of_element_with_both(driver):
name = "John Doe"
first = EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, name.split(' ', 1)[0]))(driver)
second = EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, name.split(' ', 1)[1]))(driver)
if first == second:
# Both elements exist and they are the same
return first
else:
return False
这将尝试找到一个带有部分链接文本“John”的元素,然后它会尝试找到一个带有部分链接文本“Doe”的元素 - 如果两个元素都找到并且都指向同一个元素 - 你就是黄金。
你可以在你的直到像这样使用它-
WebDriverWait(driver, 10).until(presence_of_element_with_both)
但是,您可能会发现概括这一点很方便-
def presence_of_element_with_all(locators):
def inner(driver):
# Get all the elements that match each given locator
results = [EC.presence_of_element_located(locator)(driver) for locator in locators]
# Check if all the elements are the same
# If they are, all the conditions have been met
# If they are not, all the conditions did not meet
return results[0] if len(set(results)) == 1 else False
return inner
这将找到满足所有给定定位器的奇异元素。
你可以像这样使用它-
first_name, last_name = "John Doe".split(' ')
WebDriverWait(driver, 10).until(presence_of_element_with_all([(By.PARTIAL_LINK_TEXT, first_name), (By.PARTIAL_LINK_TEXT, last_name)]))
请注意,我正在使用闭包模式来执行此操作。在内部,使用selenium
aclass
和函数来做同样的事情 - 这被称为对象之类的函数,如果你愿意,你也可以使用它 -__init__
__call__
class presence_of_element_with_all:
def __init__(self, locators):
self.locators = locators
def __call__(self, driver):
results = [EC.presence_of_element_located(locator)(driver) for locator in self.locators]
# Check if all the elements are the same
# If they are, all the conditions have been met
# If they are not, all the conditions did not meet
return results[0] if len(set(results)) == 1 else False
您可以使用与闭包模式完全相同的方式。