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? 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.