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

发布于 2022-09-02 12:43:17 字数 2198 浏览 9 评论 0

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

错误截图如下

clipboard.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2022-09-09 12:43:17

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文