Python Forum
Getting error str object is not callable in find by xpath
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting error str object is not callable in find by xpath
#1
Here is my code segment -

for clk in range(1, int(lim)):
    for i in range(1, 101):
        tab_str = '//*[@id="tbody_other_search"]/tr['
        tab_str = tab_str + str(i) + ']/td[1]'
        print(tab_str)
        driver.find_element_by_xpath(tab_str).text()
        '''if int(num) % 100 == int(en_dig):
            print(num)'''
        driver.find_element_by_xpath('//*[@id="names_load_next"]/a/i').click()
    WebDriverWait(driver,5)
I confirmed that the correct xpath is generated in tab_str by the printing the xpath but getting the error for the line -
driver.find_element_by_xpath(tab_str).text()
and here is the error -
Error:
driver.find_element_by_xpath(tab_str).text() TypeError: 'str' object is not callable
What is the reason of error? please share with me.
Reply
#2
You are trying to get text by calling it as a function, but it's just a string.

Use this instead:
driver.find_element_by_xpath(tab_str).text
Reply
#3
(Aug-14-2018, 10:56 AM)mlieqo Wrote: You are trying to get text by calling it as a function, but it's just a string.

Use this instead:
driver.find_element_by_xpath(tab_str).text

This is not a good opinion because I also tried this but getting suggestion in IDE that "This statement seems no effect". The .text will not be fruitful until it is not assigned to any variable. I used the statement like below -
driver.find_element_by_xpath(tab_str)
but the error was also arise.
Now I find the reason behind the error, I did a little mistake with the indentation the code should be like below instead of the previous one I post-
for clk in range(1, int(lim)):
    for i in range(1, 101):
        tab_str = '//*[@id="tbody_other_search"]/tr['
        tab_str = tab_str + str(i) + ']/td[1]'
        '''print(tab_str)'''
        num = driver.find_element_by_xpath(tab_str).text
        print(num)
    driver.find_element_by_xpath('//*[@id="names_load_next"]/a/i').click()
    WebDriverWait(driver,5)
And thanks for sharing your suggestion Smile
Reply
#4
I was only explaining why are you getting the error that you wrote, what you do with it is not my concern. And the reason of your error was because you used:
driver.find_element_by_xpath(tab_str).text()
instead of
driver.find_element_by_xpath(tab_str).text
nothing else. That obviously doesn't mean that you have no other errors in your code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 513 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  TypeError: 'NoneType' object is not callable akbarza 4 1,001 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,363 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 841 Jul-23-2023, 02:25 PM
Last Post: Fare
  Error when using FIND WJSwan 2 898 Jan-27-2023, 05:49 AM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,085 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  selenium. xpath error 3lnyn0 1 1,540 Dec-26-2022, 02:49 PM
Last Post: snippsat
  'SSHClient' object is not callable 3lnyn0 1 1,172 Dec-15-2022, 03:40 AM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,456 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,921 Oct-30-2022, 12:53 PM
Last Post: makeeley

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020