首页 > 解决方案 > Getting unexpected TypeErrorwhen trying to find element on page by text

问题描述

I am using a wait function to make Selenium search for the presence of an element on the page by its text before it continues to take actions. I am trying to pass the text that I am searching for into my function, however, I keep getting "TypeError: 'str' object is not callable", which is odd because I am looking for a string! I've tried everything I can think of, even escape sequences, but nothing seems to make it work. Please find my code below:

self.webdriver_wait(action = 'find_text', keys_to_pass = 'Financial Documents')

def webdriver_wait(self, selector = '#body', action='', keys_to_pass=''):
    try:
            WebDriverWait(self.browser, 240).until(EC.presence_of_element_located((By.XPATH("//*[contains(text(), \' + str(keys_to_pass) + \'')]")))) 

标签: pythonpython-3.xseleniumselenium-webdriverselenium-chromedriver

解决方案


By.XPATH不是一个方法,而是一个简单的字符串(By.XPATH == 'xpath'),你需要使用它如下:

WebDriverWait(self.browser, 240).until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(), '%s')]" % keys_to_pass))) 

推荐阅读