V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
MagicalE
V2EX  ›  Python

ubuntu 服务器下 selenium 自动回复程序报错

  •  
  •   MagicalE · 2020-09-08 16:02:24 +08:00 · 1551 次点击
    这是一个创建于 1317 天前的主题,其中的信息可能已经有所发展或是发生改变。

    小白刚开始学习 python,打算写个贴吧自动回复的程序练练手。

    本来在 Windows 下已经调试好了,原代码如下:

    from selenium import webdriver
    from time import sleep
    
    def cookie():
        cookies='''
        BDUSS=xxxx
        STOKEN=xxxx
        '''
        for lines in cookies.split():
            name = lines.split('=')[0]
            value = lines.split('=')[1]
            browser.add_cookie({"name":name,"value":value})
        browser.refresh()
    
    def reply():
        content = 'test'
        js = "document.getElementById('ueditor_replace').innerHTML='%s'" % content
        browser.execute_script(js)
        browser.find_element_by_css_selector('.poster_submit').click()
    
    def main():
        browser.get('https://tieba.baidu.com/p/xxxxxxxxx')
        sleep(5)
        cookie()
        browser.execute_script("window.scrollTo(0,document.body.scrollHeight);")
        sleep(5)
        reply()
        sleep(5)
        browser.close()
    
    browser = webdriver.Chrome()
    if __name__ == '__main__':
        main()
    

    放在 Ubuntu 服务器上却有些问题>﹏<

    由于服务器没有图形界面,运行时会报错:

    selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
    

    查了一下需要设置为无头浏览器模式,于是添加了以下部分:

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--no-sandbox')
    browser = webdriver.Chrome(options=chrome_options)
    

    可以正常运行 selenium 后,又报 JS 代码的错误

    selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot set property 'innerHTML' of null
    

    说 reply 部分的 JS 代码将 innerHTML 设置为空,但我没有设置为空呀,不懂 JS,真心求教!

    第 1 条附言  ·  2020-09-09 15:21:18 +08:00

    昨天怕是JS代码的问题,我将reply部分改为使用add_key添加回复内容了,windows下依旧正常使用

        browser.find_element_by_id('ueditor_replace').click
        input_box = browser.find_element_by_id('ueditor_replace')
        input_box.send_keys('TEST')
        sleep(5)
        browser.find_element_by_css_selector('.poster_submit').click()
    

    但linux下还是报错:

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="ueditor_replace"]"}
    

    稍微学了下headless chrome的调试,发现使用headless模式的时候访问需要验证,可能是被百度检测到了,所以linux下的程序无法正常访问贴吧,导致识别不出元素。

    希望大佬们能指点一下(/ω\)

    10 条回复    2020-09-10 11:00:23 +08:00
    nullboy
        1
    nullboy  
       2020-09-09 10:37:19 +08:00 via Android   ❤️ 1
    selenium 用 docker 起啊,用 remote driver
    MagicalE
        2
    MagicalE  
    OP
       2020-09-09 11:35:10 +08:00
    @nullboy 谢谢,我了解一下。
    感觉程序报错可能是 headless chrome 的问题,Windows 下面直接调用 chrome 是没问题的。。
    teddy2725
        3
    teddy2725  
       2020-09-09 11:49:59 +08:00   ❤️ 1
    需要搞个 virtual diplay
    musi
        4
    musi  
       2020-09-09 13:15:51 +08:00 via iPhone   ❤️ 1
    你是英语不好么,说的是不能设置 null 对象的 innerHTML 属性。。。
    MagicalE
        5
    MagicalE  
    OP
       2020-09-09 13:41:34 +08:00
    @teddy2725 好的谢谢,我也了解一下。
    MagicalE
        6
    MagicalE  
    OP
       2020-09-09 13:53:04 +08:00
    @musi 哈哈,上大学以后英语用太少了(不过最近也开始背托福单词了)
    第一眼看到这个报错我也翻译的是 null 的 innerHTML,但不了解 js 觉得 null 是形容词感觉怪怪的,碰巧划词软件翻译的是"无法将属性“ innerHTML”设置为 null",就顺着这个意思了。
    那么照原本的意思来看是因为 headless 模式下的 chrome 不能设置 null 的属性嘛? Windows 下的 chrome 明明可以的>︿<
    MagicalE
        7
    MagicalE  
    OP
       2020-09-09 15:24:14 +08:00
    @teddy2725 pyvirtualdisplay 是为了不使用 headless 模式还是为了防止被检测呢?开启 virtual display 后,如果不使用 headless 模式还是会报错'Chrome failed to start';使用 headless 也还是报错识别不到 element (可能还是被检测到了╯︿╰)
    musi
        8
    musi  
       2020-09-09 16:08:59 +08:00
    @MagicalE 有头模式你也不能设置 null 的属性啊,这应该是你元素没有定位到的问题
    MagicalE
        9
    MagicalE  
    OP
       2020-09-09 18:10:09 +08:00
    @musi 嗯嗯,我发现无头 chrome 什么页面的元素都定位不了,还不知道怎么解决,实在不行就用 Firefox 吧。。
    teddy2725
        10
    teddy2725  
       2020-09-10 11:00:23 +08:00
    @MagicalE 是为了不使用 headless 模式 然后可以截图看网页渲染状态好 debug, 有些网页 headless 会打不开
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5650 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:33 · PVG 14:33 · LAX 23:33 · JFK 02:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.