from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
driver = webdriver.PhantomJS()
#driver = webdriver.Firefox()
driver.get('http://global.ahnlab.com/site/securitycenter/securityinsight/securityInsightList.do')
driver.execute_script("getView('2218')")
html_source = driver.page_source
driver.quit()
soup = BeautifulSoup(html_source)
print(soup.h1.string)
当我使用Firefox()时,结果是[AhnLab Puts in Appearance at RSAConference for 4th Straight Year]我想要的。 但是当我使用PhanthomJS()时,结果是[Security Insight]我不想要。
如果我使用PhantomJS(),我无法得到我想要的结果? 我希望使用无头浏览器获得第一个结果。
感谢。
答案 0 :(得分:3)
在立即调用javascript后,phantomjs驱动程序未加载导航。只需在javascript调用后进行5-10秒的睡眠,它就应该适合你。
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
driver = webdriver.PhantomJS()
#driver = webdriver.Firefox()
driver.get('http://global.ahnlab.com/site/securitycenter/securityinsight/securityInsightList.do')
driver.execute_script("getView('2218')")
# Introduce a sleep of 5 seconds here
time.sleep(5)
html_source = driver.page_source
driver.quit()
soup = BeautifulSoup(html_source)
print(soup.h1.string)