python_selenium使用时,在切换无头模式后报错的原因

在服务器上可以运行页面,但是要由于没有显示器,所以要修改为无头模式,修改时出现问题,某些按钮无法点击,最后发现原因为:在代码中最大化时的分辨率有问题,导致无法点击按钮

在改为无头模式前,代码一切运行正常,增加下面代码后

option.add_argument('--headless')

报错:

Traceback (most recent call last):
  File "C:\Users\Windows\Desktop\deyu\pythonProject1\send4_linux.py", line 90, in <module>
    reply()#模拟一次点击
  File "C:\Users\Windows\Desktop\deyu\pythonProject1\send4_linux.py", line 53, in reply
    driver1.find_element(By.XPATH, "//div[@class='tblalm-space-item'][3]/button[@class='tblalm-btn css-1b34mjl tblalm-btn-round tblalm-btn-primary']/span").click()
  File "C:\Users\Windows\Desktop\deyu\pythonProject1\.venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 94, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Windows\Desktop\deyu\pythonProject1\.venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Windows\Desktop\deyu\pythonProject1\.venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Windows\Desktop\deyu\pythonProject1\.venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome-headless-shell=124.0.6367.202)
Stacktrace:
	GetHandleVerifier [0x00007FF725EB1522+60802]
	(No symbol) [0x00007FF725E2AC22]
	(No symbol) [0x00007FF725CE7B13]
	(No symbol) [0x00007FF725D38052]
	(No symbol) [0x00007FF725D2AE8B]
	(No symbol) [0x00007FF725D5AB7A]
	(No symbol) [0x00007FF725D2A7C6]
	(No symbol) [0x00007FF725D5AD90]
	(No symbol) [0x00007FF725D7A224]
	(No symbol) [0x00007FF725D5A923]
	(No symbol) [0x00007FF725D28FEC]
	(No symbol) [0x00007FF725D29C21]
	GetHandleVerifier [0x00007FF7261B41BD+3217949]
	GetHandleVerifier [0x00007FF7261F6157+3488183]

解决方法:

        option.add_argument('--headless')
        option.add_argument('--no-sandbox')
        option.add_argument('--disable-dev-shm-usage')
        option.add_argument("--window-size=1920,1080");

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值