首页 > 解决方案 > 'str' 对象在使用按类查找元素时不可调用 | 蟒蛇硒

问题描述

当我正在编写以下代码时,无法单击按钮。它显示错误“TypeError:'str'对象不可调用”

代码

   shopping_btn = self.driver.find_element(By.CLASS_NAME("cta cta-hover-animate cta-fill-black-outline-black"))
   shopping_btn.click()

错误:

文件“E:\Python\setests\smoketest.py”,第 33 行,在 test_shopping_cart_empty_message shopping_btn = self.driver.find_element(By.CLASS_NAME("cta cta-hover-animate cta-fill-black-outline-black")) TypeError:“str”对象不可调用

标签: pythonselenium

解决方案


你需要像这样使用 find_element :

shopping_btn = self.driver.find_element(By.CLASS_NAME, "cta cta-hover-animate cta-fill-black-outline-black")
shopping_btn.click()

由于尝试调用而出现异常

By.CLASS_NAME('<your_selector>')

什么是平等的

"any_string"('<you_selector>')

推荐阅读