importseleniumfromseleniumimportwebdriverfromselenium.common.exceptionsimportNoSuchElementExceptionfromselenium.webdriver.common.keysimportKeysimporttimebrowser=webdriver.Firefox()# Get local session of firefoxbrowser.get("http://news.sina.com.cn/c/2013-07-11/175827642839.shtml ")# Load pagetime.sleep(5)# Let the page loadtry:element=browser.find_element_by_xpath("//span[contains(@class,'f_red')]")# get element on pageprintelement.text# get element textexceptNoSuchElementException:assert0,"can't find f_red"browser.close()
>>> from selenium import webdriver>>> c = webdriver.Chrome()>>> c.get('http://news.sina.com.cn/c/2013-07-11/175827642839.shtml')>>> comment = c.find_element_by_id('media_comment')>>> count = comment.find_element_by_class_name('f_red')>>> count.textu'823'