使用.text()复制文本硒

时间:2018-07-06 04:04:25

标签: python selenium

我需要使用硒从表格中抓取一些文本。我是一个新手,但是由于Stack溢出,我可以走得很远。现在,使用text()复制文本时出现错误。这是我的代码。

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

现在您可以找到下面的表格。我想使用以下代码复制文本:

driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text()

但是我得到了:

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

我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

使用

driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text

代替

driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text()

语句driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text返回一个string,因此您不必在末尾添加括号。如果添加括号,则语法意味着您正在调用,但是您只能 call callable 对象,例如函数。因此,错误。