Python-浏览器不兼容错误line 479

本文介绍了一个使用Selenium WebDriver进行网页自动化测试的例子。通过Python脚本操作Firefox浏览器时遇到元素输入错误的问题,并给出了使用IE浏览器成功执行的示例。

代码如下:
#encoding=utf-8
from selenium import webdriver
import time

driver = webdriver.Firefox(executable_path="G:\\geckodriver")

driver.get("http://www.sogo.com")
driver.find_element_by_id("query").clear()

driver.find_element_by_id("query").send_keys("test")
driver.find_element_by_id("stb").click()

time.sleep(3)
driver.quit()
执行报错:
Traceback (most recent call last):
  File "F:/MyProject/test.py/tesst.py", line 10, in <module>
    driver.find_element_by_id("query").send_keys("test")
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: Expected [object Undefined] undefined to be a string
换成IE,执行成功
#encoding=utf-8
from selenium import webdriver
import time

driver = webdriver.Ie(executable_path="G:\\IEDriverServer")

driver.get("http://www.sogo.com")
driver.find_element_by_id("query").clear()

driver.find_element_by_id("query").send_keys("test")
driver.find_element_by_id("stb").click()

time.sleep(3)
driver.quit()


(.venv) root@lujh:/xhs-toolkit# python xhs_toolkit.py cookie save python-dotenv could not parse statement starting at line 31 python-dotenv could not parse statement starting at line 34 python-dotenv could not parse statement starting at line 35 python-dotenv could not parse statement starting at line 36 python-dotenv could not parse statement starting at line 37 python-dotenv could not parse statement starting at line 38 python-dotenv could not parse statement starting at line 39 python-dotenv could not parse statement starting at line 31 python-dotenv could not parse statement starting at line 34 python-dotenv could not parse statement starting at line 35 python-dotenv could not parse statement starting at line 36 python-dotenv could not parse statement starting at line 37 python-dotenv could not parse statement starting at line 38 python-dotenv could not parse statement starting at line 39 ╭─────────────────────────────────────────╮ │ 小红书MCP工具包 │ │ Xiaohongshu MCP Toolkit v1.2.3 │ ╰─────────────────────────────────────────╯ 🍪 执行Cookie操作: save python-dotenv could not parse statement starting at line 31 python-dotenv could not parse statement starting at line 34 python-dotenv could not parse statement starting at line 35 python-dotenv could not parse statement starting at line 36 python-dotenv could not parse statement starting at line 37 python-dotenv could not parse statement starting at line 38 python-dotenv could not parse statement starting at line 39 📝 注意:新版本直接获取创作者中心权限cookies 🔧 这将解决跳转到创作者中心时cookies失效的问题 12:30:18 | INFO | 🌺 开始获取小红书创作者中心Cookies... 12:30:18 | INFO | 📝 注意:将直接跳转到创作者登录页面,确保获取完整的创作者权限cookies 12:30:18 | INFO | 🚀 初始化Chrome浏览器驱动... 12:30:18 | INFO | 🖥️ 启用有界面浏览器模式 ❌ Cookie操作失败: 获取cookies过程出错: 创建Chrome驱动失败: Message: session not created from unknown error: no chrome binary at /usr/bin/google-chrome Stacktrace: #0 0x61a597e1767a <unknown> #1 0x61a5978e3cd0 <unknown> #2 0x61a59791a947 <unknown> #3 0x61a597918d58 <unknown> #4 0x61a597968464 <unknown> #5 0x61a597967b86 <unknown> #6 0x61a5979599f3 <unknown> #7 0x61a597926534 <unknown> #8 0x61a597927191 <unknown> #9 0x61a597ddeeef <unknown> #10 0x61a597de2f98 <unknown> #11 0x61a597dc60f2 <unknown> #12 0x61a597de373e <unknown> #13 0x61a597dad0be <unknown> #14 0x61a597e05c68 <unknown> #15 0x61a597e05e78 <unknown> #16 0x61a597e1611c <unknown> #17 0x78ffac494ac3 <unknown> 💡 建议: 请检查登录状态或重新获取cookies
最新发布
06-25
Python 3.13.3 (tags/v3.13.3:6280bb5, Apr 8 2025, 14:47:33) [MSC v.1943 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from selenium import webdriver >>> from selenium.webdriver.chrome.service import Service >>> from webdriver_manager.chrome import ChromeDriverManager >>> >>> # 自动安装驱动并创建浏览器实例 >>> def create_driver(): ... # 自动下载匹配版本的ChromeDriver ... driver_path = ChromeDriverManager().install() ... >>> # 创建服务配置(必须使用Service类) >>> service = Service(executable_path=driver_path) File "<python-input-7>", line 1 service = Service(executable_path=driver_path) IndentationError: unexpected indent >>> >>> # 初始化浏览器选项 >>> options = webdriver.ChromeOptions() File "<python-input-10>", line 1 options = webdriver.ChromeOptions() IndentationError: unexpected indent >>> options.add_argument("--start-maximized") # 窗口最大化 File "<python-input-11>", line 1 options.add_argument("--start-maximized") # 窗口最大化 IndentationError: unexpected indent >>> >>> # 创建浏览器实例 >>> driver = webdriver.Chrome(service=service, options=options) File "<python-input-14>", line 1 driver = webdriver.Chrome(service=service, options=options) IndentationError: unexpected indent >>> return driver File "<python-input-15>", line 1 return driver IndentationError: unexpected indent >>> >>> # 测试用例 >>> def test_baidu_search(): ... driver = create_driver() ... try: ... driver.get("https://www.baidu.com") ... print("当前页面标题:", driver.title) ... assert "百度" in driver.title ... finally: ... driver.quit() ... File "<python-input-18>", line 3 try: IndentationError: unexpected indent >>> if __name__ == "__main__": ... test_baidu_search() ... Traceback (most recent call last): File "<python-input-19>", line 2, in <module> test_baidu_search() ^^^^^^^^^^^^^^^^^ NameError: name 'test_baidu_search' is not defined >>>为什么没有反应
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值