求教:如何让 selenium 在 headless 环境处理有延时才能点击的按钮 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
yaleyu

求教:如何让 selenium 在 headless 环境处理有延时才能点击的按钮

  •  
  •   yaleyu Jul 11, 2023 2310 views
    This topic created in 1023 days ago, the information mentioned may be changed or developed.

    有的页面打开有个按钮是不可点击,要延时几秒才会变成可点击状态,然后要处理点击后的页面。比如这个 https://ouo.io/go/yq25z

    from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'bn-main'))) element.click() 

    在无图形界面的 Ubuntu Server 上运行,加了chrome_options.add_argument('--headless')就不工作,加上 time.sleep(15)也不行。

    在 macOS 上运行,不加--headless ,每次运行自动打开 Chrome 就能正常处理后续的工作。

    请问怎么让它在无图形界面的环境也能工作?

    9 replies    2023-07-12 18:29:24 +08:00
    jjplay
        1
    jjplay  
       Jul 11, 2023
    装虚拟屏幕 xvfb
    yaleyu
        2
    yaleyu  
    OP
       Jul 11, 2023
    @jjplay 赞!所以说,selenium 的某些功能就不能无头?我以为是没用对
    wgit
        3
    wgit  
       Jul 11, 2023
    不是可以等待某个元素吗 等待多少秒,然后间隔多久检查一次
    还能基于标签属性 去判断按钮是否是可用状态
    yaleyu
        4
    yaleyu  
    OP
       Jul 11, 2023
    @wgit 嗯,这些都做了,现在的问题是在无头环境下不工作,有头环境下就一切正常。
    harrozze
        5
    harrozze  
       Jul 11, 2023
    @yaleyu #4 过若干秒截屏,看看按钮的视觉状态是什么。同时把按钮的 html computed 以后的属性打出来看看
    girl1462235
        6
    girl1462235  
       Jul 11, 2023
    其实。。。 你设置一个 UserAgent 就好了
    girl1462235
        7
    girl1462235  
       Jul 11, 2023
    yaleyu
        8
    yaleyu  
    OP
       Jul 12, 2023
    @girl1462235 哎,真的行,看来页面做了一定的反爬,有头时候是真正打开浏览器,无头时候被反爬了
    yaleyu
        9
    yaleyu  
    OP
       Jul 12, 2023
    @yaleyu 弄错了,再试了几次,无头还是不行,xvfb 加 pyvirtualdisplay 应该是我想要的解决方式。
    About     Help     Advertise     Blog     API     FAQ     Solana     5578 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 67ms UTC 03:05 PVG 11:05 LAX 20:05 JFK 23:05
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86