【问题标题】:Python, Selenium: How to check existence of an element?Python,Selenium:如何检查元素的存在?
【发布时间】:2016-01-04 10:58:35
【问题描述】:

我试图一次浏览一个网站,点击元素next,在HTML 代码中是class="pg-next"。不过,最终我会走到尽头,不会再有next 元素,此时我想停止循环。我有这样的事情:

pg_next_exists = True
while pg_next_exists:
    #
    # carry out necessary actions
    #
    if ...: # if the pg-next element can be found
        pass
    else:
        pg_next_exists = False # at which point the while loop stops

如何检查该元素是否仍然存在?

【问题讨论】:

标签: python html selenium


【解决方案1】:

你为什么不使用wait试试这样的东西

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Firefox()
browser.get('http://example.com/')

try:
     wait = WebDriverWait(browser, 10) 
     search = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'next')))
     search.click()
except:
     # Process element not found here

【讨论】:

  • 创建变量search 的意义何在?你什么都不用做
猜你喜欢
  • 2018-01-23
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多