2
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.

1
  • Did my answer work for you?
    – Vikas Ojha
    Commented Jun 19, 2015 at 22:03

1 Answer 1

5

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)
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.