Reputation: 35
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)
When I use Firefox(), the result is [AhnLab Puts in Appearance at RSAConference for 4th Straight Year] that I want. But when I use PhanthomJS(), the result is [Security Insight] that I don't want.
If I use PhantomJS(), I can't get the result that I want? I want to get the first result using a headless browser.
thanks.
Upvotes: 2
Views: 6209
Reputation: 6950
The phantomjs driver is not loading the navigation after the javascript call immediately. Just put a sleep of 5-10 seconds after the javascript call and it should work for you.
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)
Upvotes: 5