Reputation: 383
I need to scrape some text from a table, using selenium. I'm a complete novice, but thanks to Stack overflow, I was able to get to get pretty far. Now I am getting an error when using text() to copy text. Here are my codes.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
driver = webdriver.Chrome('C:/Users/User/Downloads/chromedriver')
url = "https://aca.tampagov.net/citizenaccess/Default.aspx#"
driver.get(url)
# The website I need to check
# Click "search" at the top menu
driver.find_element_by_xpath('//*[@id="main_menu"]/ul/li[2]/a').click()
# Click "building permits" in the menu that follows below
driver.find_element_by_xpath('//[@id="main_menu"]/ul/li[2]/ul/li[1]/a').click()
#change iframes
driver.switch_to.frame("ACAFrame")
# When changing iframes, I was recommended that I should use this,
# WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//*[@id='ACAFrame']")))
# but I'm now having troubles running this line so I changed it to the line like above
Now you can find that there's a table below. I want to copy text using a code such as:
driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text()
But I get, instead:
Traceback (most recent call last):
File "<ipython-input-117-2a4c7f9321b5>", line 1, in <module>
driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text()
TypeError: 'str' object is not callable
What am I doing wrong here?
Upvotes: 0
Views: 1179
Reputation: 59264
Use
driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text
instead of
driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text()
The statement driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text
returns a string
, so you don't have to add the parenthesis at the end. If you add the parenthesis, then syntax means you are calling it, but you can only call callable objects, such as functions. Thus, the error.
Upvotes: 1