首页 > 用phantomjs时超时报错,用谷歌浏览器没有问题,这是什么原因

用phantomjs时超时报错,用谷歌浏览器没有问题,这是什么原因

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree

class autoTiebaComment(unittest.TestCase):
    def setUp(self):
        phantomjs_path = r"D:\Program Files\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin\phantomjs.exe"
        self.driver = webdriver.PhantomJS(executable_path=phantomjs_path,)
        #self.driver = webdriver.Chrome()
    def testAutoComment(self):
        driver = self.driver
        url = "http://tieba.baidu.com/f?kw=%CE%F7%C4%CF%BD%BB%CD%A8%B4%F3%D1%A7&fr=index"
        driver.get(url)
        print('打开页面')
        bcookie = driver.get_cookies()
        driver.maximize_window()
        WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "u_login"))).click()
        print('正在登录。。。')
        WebDriverWait(driver, 25).until(EC.presence_of_element_located((By.NAME, "userName"))).send_keys('######')
        driver.find_element_by_name('password').send_keys('******')
        elebtn = driver.find_element_by_id('TANGRAM__PSP_9__submit').click()
        acookie = driver.get_cookies()
        while bcookie == acookie:
            verifycode = input('登录失败,请输入验证码:')
            elebtn.click()
        print('登录成功!')
        driver.refresh()
        html = etree.HTML(driver.page_source)
        lista = html.xpath('//div[@class="threadlist_text threadlist_title j_th_tit  "]/a/@href')
        print('获得列表')
        baseurl = 'http://tieba.baidu.com'
        for item in lista:
            url = baseurl + item
            driver.get(url)
            print('打开一个页面%s' % item)
            driver.find_element_by_xpath('//div[@id="ueditor_replace"]').send_keys('!!!')
            driver.find_element_by_xpath('//div[@class="j_floating"]/a').click()
            print('发布成功!')

    def tearDown(self):
        self.driver.close()
if __name__ == "__main__":
    unittest.main()

错误截图如下


phantomjs有超时选项,把它设置长一点。或者有一些资源访问不到,这个需要设置代理。

【热门文章】
【热门文章】