硒合二为一的xpath测试 [英] selenium two xpath tests in one

查看:93
本文介绍了硒合二为一的xpath测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to combine check for two scenarios:

如果启动检查失败,我们将提供一个重试按钮:

If startupcheck fails we get a try again button:

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

或者启动检查成功,我们将在一个自定义对象中收到一个pin输入请求:

Or startupcheck succeed we get a pin enter request in a custom object:

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

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

How can this be combined into one check without having to check for both: I tried the following:

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

但是仅检查第一个or语句.

But only the first or statement is checked.

推荐答案

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

You can club up combine check for both the elements using OR clause through a lambda expression as follows:

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"))


作为替代方案,您可以使用等效的,如下所示:


As an alternative you can club up combine check for both the elements using the equivalent css-selectors as follows:

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


参考文献

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆