execute_script()在带有硒phantomjs的python中不起作用

pau*_*aul 2 javascript python selenium phantomjs

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)
Run Code Online (Sandbox Code Playgroud)

当我使用Firefox()时,结果是我想要的[AhnLab连续第四年参加RSA会议。但是,当我使用PhanthomJS()时,结果是我不需要的[Security Insight]。

如果使用PhantomJS(),则无法获得所需的结果?我想使用无头浏览器获得第一个结果。

谢谢。

Vik*_*jha 5

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)
Run Code Online (Sandbox Code Playgroud)