我使用 Python/Selenium 提交表单,然后让 Web 驱动程序使用类 ID 使用预期条件等待下一页加载。
我的问题是有两个页面可以显示,但它们不共享原始页面中不存在的唯一元素(我可以找到)。一页有一个独特的类是 mobile_txt_holder
另一个可能的页面的类 ID 为 notfoundcopy
. 我想使用正在寻找 mobile_txt_holder
的等待或者 notfoundcopy
出现。
是否可以将两个预期条件合并为一个等待条件?
我正在寻找但显然行不通的基本想法:
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")))
我真的只需要编程等待下一页加载,这样我就可以解析源代码。
示例 HTML:
<p class="notfoundcopy">Unfortunately, the number you entered is not in our tracking system.</p>
最佳答案
除了通过 or
子句组合 2 个 expected_conditions
之外,我们还可以轻松构造一个 CSS
来满足我们的需求。下面的 CSS
将在 mobile_txt_holder
类或 notfoundcopy
类中寻找 EC:
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
关于python - WebDriverWait 预期条件中的逻辑运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46147145/