【问题标题】:selenium two xpath tests in oneselenium 两个 xpath 测试合二为一
【发布时间】:2020-01-13 08:50:48
【问题描述】:

我尝试结合检查两种情况:

如果启动检查失败,我们会得到一个重试按钮:

el = WebDriverWait(self.driver, 10).until(
  EC.element_to_be_clickable((By.NAME, "Try again")))

或者 startupcheck 成功我们在一个自定义对象中得到一个 pin 输入请求:

el = WebDriverWait(self.driver, 20).until(
  EC.element_to_be_clickable((By.XPATH, "//Custom/Edit")))

如何将其合并为一项检查而不必同时检查两者: 我尝试了以下方法:

check = WebDriverWait(self.driver, 20).until(
  EC.element_to_be_clickable(
    (By.XPATH, "//Custom/Edit") or (By.NAME, "Try again")
))

但只检查第一个or 语句。

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    您可以使用 OR 子句通过 lambda 表达式对这两个元素进行组合检查,如下所示:

    el = WebDriverWait(driver, 20).until(lambda x: (x.find_element_by_name("Try again"), x.find_element_by_xpath("//Custom/Edit")))
    

    另一种解决方案是:

    el = WebDriverWait(driver,20).until(lambda driver: driver.find_element(By.NAME,"Try again") and driver.find_element(By.XPATH,"//Custom/Edit"))
    

    作为替代方案,您可以使用等效的 对这两个元素进行组合检查,如下所示:

    el = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='Try again'], Custom>Edit")))
    

    参考文献

    【讨论】:

    • 太棒了,我希望这是在selenium-python.readthedocs.io的官方文档中
    • 两个问题:我不熟悉“使用 OR 子句加入俱乐部”这个术语。那是什么意思?另外,在“替代示例”代码中,为什么条件之间是“和”而不是“或”?
    • @rothloup 这些是等待预期条件的 lambda 表达式。对于更简单的情况,请参阅讨论Selenium's find_elements with two clauses for text?
    • @DebanjanB:我知道它们是 lambda 表达式。我不明白“club up”是什么意思(听起来像口语,谷歌搜索的结果并不多),也不明白为什么用“AND”检查“OR”条件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2015-12-30
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多