如何将Selenium与Chrome代码转换为PhantomJS?

时间:2018-02-13 18:05:24

标签: python selenium phantomjs

我已经编写了一些代码来使用selenium来抓取网页。如果我使用Chrome网络驱动程序,它工作正常。但是,如果我将其更改为PhantomJS(),我会收到错误,说没有这样的元素异常。代码是:

from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from time import sleep
s = requests.session()
driver = webdriver.Chrome('F:\chromedriver')
driver.get("https://in.bookmyshow.com/booktickets/VMAX/2061")
sleep(40)
# To switch to frame
driver.switch_to.frame(driver.find_element_by_id("wiz-iframe"))

# Clicking on the element inside the frame
e2 = driver.find_element_by_xpath("//div[@class='wzrkPPwarp']//a")
e2.click()

# Switching back to main content
driver.switch_to_default_content()

# Then only we can access elements
e3 = driver.find_element_by_xpath("//button[@class='No thanks']")
e3.click()

这是使用Chrome网络驱动程序编写的代码。当我改为:

driver = webdriver.PhantomJS()

我收到如下错误:

  

NoSuchElementException:消息:{" errorMessage":"无法找到带有xpath的元素' // div [@class =' wzrkPPwarp'] // a& #39;""请求" {"头" {"接受":"应用/ JSON"&# 34;接受编码":"身份""连接":"靠近"" Content-Length的":&#34 ; 113""内容类型":"应用/ JSON;字符集= UTF-8""主机":" 127.0.0.1 :56829"," User-Agent":" Python http auth"}," httpVersion":" 1.1",&# 34;方法":" POST"," post":" {\"使用\":\" xpath \& #34;,\"值\":\" // div [@class =' wzrkPPwarp'] // a \",\&#34 ; sessionId \":\" ccc33320-10e5-11e8-b5fa-dbfae1ffdb07 \"}"," url":" / element&#34 ;," urlParsed" {"锚":"""查询":""&# 34;文件":"元件""目录":" /""路径":" /元素& #34;" REL ative":" /元件""端口":"""主机":"" "密码":"""使用者":""" USERINFO":" #&34;,"权威":"""协议":"""源&#34 ;: " /元件"" queryKey":{},"大块":["元件"]}" urlOriginal&#34 ;:" /会话/ ccc33320-10e5-11e8-b5fa-dbfae1ffdb07 /元件"}}       屏幕截图:可通过屏幕获取

如何使其正确?请帮忙。谢谢!

0 个答案:

没有答案