嗨,我是Selenium的新手,非常感谢您的帮助。
我需要学习如何复制文本,尤其是从特定网站的表格中复制文本:https://aca.tampagov.net/citizenaccess/Default.aspx# (转到url->搜索->建筑许可->单击搜索按钮。)
您可以在页面底部找到一个表格。
“记录类型”列中的第一行具有文本“ Commercial Utility Application”。如果我想复制文本,我认为我应该做的就是获取xpath,即
//*[@id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList_ctl02_lblType"]
所以我会有类似
driver.find_element_by_xpath('//*[@id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
但这给了我错误。我在这里做错了什么?以及如何从这些表格中复制文本?
编辑
这就是我得到的:
Traceback (most recent call last):
File "<ipython-input-81-4e51d9229198>", line 1, in <module>
driver.find_element_by_xpath('//* [@id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 387, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 957, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]"}
(Session info: chrome=67.0.3396.99) (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)
答案 0 :(得分:1)
要检索文本Commercial Utility Application,您应该尝试-
driver.findElement(By.id("ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList_ctl02_lblType")).getText();
答案 1 :(得分:1)
您的表格位于iframe
中,因此您必须先切换到该表格,然后才能与此表格进行交互。此代码段将帮助您做到这一点:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//*[@id='ACAFrame']")))
# do your stuff
text = driver.find_element_by_xpath('//*[@id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
print(text)
driver.switch_to.default_content() # switch back to default content
PS:由于页面中只有一个表,因此您可以从以下方式简化xPath:
//*[@id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]
收件人:
//tbody/tr[3]/td[4]