selenium two xpath tests in one

非 Y 不嫁゛ 提交于 2019-12-24 07:49:14

问题


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

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

But only the first or statement is checked.


回答1:


You can club up combine check for both the elements using xpath 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")))

An alternative xpath will be:

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

References

  • Python / Selenium: Logic Operators in WebDriverWait Expected Conditions
  • How to extract dynamic text from multiple child nodes within the html through getText()


来源:https://stackoverflow.com/questions/57890400/selenium-two-xpath-tests-in-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!