Python类编写Selenium认证机器人时出现'str'对象不可调用错误
问题:Selenium认证机器人启动后无操作,抛出
TypeError: 'str' object is not callable错误 终端报错信息
PS E:*my path*1> & E:/Python/python.exe e:*my path*/1/main.py e:*my path*1\main.py:32: DeprecationWarning: executable_path has been deprecated, please pass in a Service object self.browser = webdriver.Chrome(ChromeDriverManager().install(),options=options) DevTools listening on ws://127.0.0.1:54545/devtools/browser/f9204f7e-ffbc-4ca1-b8f0-755537aba91f Traceback (most recent call last): File "e:*my path*\1\main.py", line 76, in <module> bot.login() TypeError: 'str' object is not callable PS e:*my path*> [3484:9668:0113/153947.029:ERROR:device_event_log_impl.cc(215)] [15:39:47.028] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: ╧Ёшёюхфшэхээюх ъ ёшёЄхьх єёЄЁющёЄтю эх ЁрсюЄрхЄ. (0x1F) [3484:9668:0113/153947.029:ERROR:device_event_log_impl.cc(215)] [15:39:47.029] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: ╧Ёшёюхфшэхээюх ъ ёшёЄхьх єёЄЁющёЄтю эх ЁрсюЄрхЄ. (0x1F)
机器人代码
class LoginBot(): def __init__(self): #Excel reading wb = load_workbook('seo-fast_profiles.xlsx') sheet_ranges = wb['Work'] self.login = sheet_ranges['A2'].value self.email = sheet_ranges['B2'].value self.passw = sheet_ranges['C2'].value #Optins and UserAgent user = UserAgent() options = webdriver.ChromeOptions() options.add_argument(user.random) self.browser = webdriver.Chrome(ChromeDriverManager().install(), options=options) def close_browser(self): self.browser.close() self.browser.quit() def login(self): browser = self.browser browser.get("https://seo-fast.ru/login") time.sleep(random.randrange(1, 3)) username_input = browser.find_element(By.ID, "logusername") username_input.clear() username_input.send_keys(self.email) time.sleep(1) username_input.send_keys(Keys.ENTER) time.sleep(2) password_input = browser.find_element(By.ID, "logpassword") password_input.send_keys(self.passw) time.sleep(5) password_input.send_keys(Keys.ENTER) time.sleep(7) #cookies pickle.dump(browser.get_cookies(), open(f"{self.login}_cookies", "wb")) self.close_browser() def xpath_exists(self, url): browser = self.browser try: browser.find_element(By.XPATH, url) exist = True except NoSuchElementException: exist = False return exist bot = LoginBot() bot.login()
浏览器启动后的状态

尝试过的操作
- 将
bot.login方法重命名为其他名称,但问题未解决
解决方案
1. 修复TypeError核心问题
代码中在__init__方法里定义了实例变量self.login(从Excel读取的字符串值),同时又定义了同名的login方法。创建实例后,self.login会被覆盖成字符串,调用bot.login()时实际是在调用一个字符串,因此抛出错误。
解决方法:修改实例变量的名称,比如改为self.login_username,并同步修改代码中所有用到该变量的地方:
def __init__(self): # ... 其他代码不变 self.login_username = sheet_ranges['A2'].value # 重命名实例变量 # ... def login(self): # ... 其他代码不变 # 保存cookies时使用新的变量名 pickle.dump(browser.get_cookies(), open(f"{self.login_username}_cookies", "wb")) # ...
2. 解决Selenium的DeprecationWarning警告
原代码中使用executable_path参数已被废弃,需要改用Service对象:
from selenium.webdriver.chrome.service import Service # 导入Service类 def __init__(self): # ... 其他代码不变 self.browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
3. 消除USB错误提示
终端里的USB错误是Chrome驱动的常见警告,不影响脚本运行。如果想消除该警告,可在ChromeOptions中添加以下参数:
options.add_experimental_option('excludeSwitches', ['enable-logging'])
内容的提问来源于stack exchange,提问作者user20899166
相关产品推荐
相关产品推荐

