WebDriverWait for multiple conditions (OR logical evaluation)

故事扮演 提交于 2020-06-28 04:12:28

问题


Using python, the method WebDriverWait is used to wait for 1 element to be present on the webpage. How can this method be used without multiple try/except? Is there an OR option for multiple cases using this method? https://selenium-python.readthedocs.io/waits.html


回答1:


Without using multiple try/except{} to induce WebDriverWait for two elements through OR option you can use either of the following solutions:

  • Using CSS_SELECTOR:

      element = WebDriverWait(driver, 10).until(
          EC.presence_of_element_located((By.CSS_SELECTOR, ".element_A_class, .element_B_class"))
    
  • Using XPATH through lambda:

      element = WebDriverWait(driver,20).until(lambda driver: driver.find_element(By.XPATH,"element_A_xpath") or driver.find_element(By.XPATH,"element_B_xpath"))
    

Reference

You can find a couple of relevant discussions in:

  • selenium two xpath tests in one
  • Python / Selenium: Logic Operators in WebDriverWait Expected Conditions


来源:https://stackoverflow.com/questions/60496204/webdriverwait-for-multiple-conditions-or-logical-evaluation

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