You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Python报错:'str'对象无'execute'属性问题排查解决

Selenium ActionChains右键操作报错AttributeError: 'str' object has no attribute 'execute'解决方案

报错详情

执行右键点击元素操作时触发如下报错:

Traceback (most recent call last):
  File "C:\***\Tests\test_ZRLPCHS.py", line 23, in test_ZRLP
    fill_zrlp(self)
  File "C:\***\Source\Functions.py", line 1096, in fill_zrlp
    upload_dossier(self)
  File "C:\***\Source\Functions.py", line 968, in upload_dossier
    section = driver.find_element(*nat_dossier.txt_other_section)
  File "C:\***\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
AttributeError: 'str' object has no attribute 'execute'

原错误代码

业务操作代码

section = driver.find_element(*nat_dossier.txt_other_section)
action = ActionChains(driver)
action.context_click(section)
action.perform()

locators.py定位器定义

class DossierStmnt2(object):
    txt_other_section = (By.ID, "j1_66_anchor")

问题根因

报错的核心原因是调用find_element和传入ActionChains的driver变量并非有效的Selenium WebDriver实例,而是当前作用域下的一个字符串变量。
你的WebDriver实例是绑定在测试类的实例属性self.driver上的,在类成员方法中直接调用全局作用域的driver,或者被同名字符串变量覆盖时,就会触发该属性缺失报错。

修复方案

所有调用WebDriver的位置统一使用类实例绑定的self.driver即可,修复后代码如下:

def upload_dossier(self):
  print("Uploading dossier")
  self.driver.find_element(*nat_app.btn_dossier).click()
  section = self.driver.find_element(*nat_dossier.txt_other_section)
  action = ActionChains(self.driver)
  action.context_click(section)
  action.perform()

内容的提问来源于stack exchange,提问作者Artem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 18:15:00