Python / Selenium: Logic Operators in WebDriverWait Expected Conditions

只愿长相守 提交于 2020-01-09 08:10:53

问题


I'm using Python / Selenium to submit a form then I have the web driver waiting for the next page to load by using an expected condition using class id.

My problem is that there are two pages that can be displayed but they do not share an unique element (that I can find) that is not in the original page. One page has a unique class is of mobile_txt_holder and the other possible page has a class id of notfoundcopy. I would like to use a wait that is looking for mobile_txt_holder OR notfoundcopy to appear.

Is it possible to combine two expected conditions into one wait?

Basic idea of what I am looking for but obviously won't work:

    WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "mobile_txt_holder")))
 or .until(EC.presence_of_element_located((By.CLASS_NAME, "notfoundcopy")))

I really just need to program to wait until the next page loads so that I can parse the source.

Sample HTML:

<p class="notfoundcopy">Unfortunately, the number you entered is not in our tracking system.</p>


回答1:


Apart from clubbing up 2 expected_conditions through or clause, we can easily construct a CSS to take care of our requirement The following CSS will look either for the EC either in mobile_txt_holder class or in notfoundcopy class:

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, ".mobile_txt_holder, .notfoundcopy"))

You can find a detailed discussion in selenium two xpath tests in one



来源:https://stackoverflow.com/questions/46147145/python-selenium-logic-operators-in-webdriverwait-expected-conditions

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