TypeError: 'str' object is not callable in selenium python

TypeError: 'str' object is not callable in selenium python

class Locators:
    def locators(self):
        driver = webdriver.Chrome("C:\seldriver\chromedriver.exe")
        driver.get("http://automationpractice.com/index.php")
        driver.maximize_window()
        driver.find_element(By.ID("search_query_top" )).send_keys("T-shirts")
        driver.find_element(By.NAME("submit_search")).click()

我在尝试通过 python-selenium

中的简单 ID 定位元素时遇到此错误

错误

Traceback (most recent call last):
  File "C:/Users/abdul_saboor/PycharmProjects/nopcommerceApp/.pytest_cache/locatorsdemo1.py", line 25, in <module>
    obj_Locators.locators()
  File "C:/Users/abdul_saboor/PycharmProjects/nopcommerceApp/.pytest_cache/locatorsdemo1.py", line 18, in locators
    driver.find_element(By.ID("search_query_top" )).send_keys("T-shirts")
TypeError: 'str' object is not callable
find_element(By.ID("search_query_top" ))

这是错误的用法。 By.ID 不是一个单独的函数。

改用这个:

find_element(By.ID, "search_query_top" )