[英]HTML element not being printed with python's .text
from selenium import webdriver
import selenium.webdriver.common.keys
def main():
PATH = r"D:\msedgedriver"
driver = webdriver.Edge(PATH)
driver.get("https://reservation.frontdesksuite.ca/rcfs/richcraftkanata/Home/Index?Culture=en&PageId=b3b9b36f-8401-466d-b4c4-19eb5547b43a&ButtonId=00000000-0000-0000-0000-000000000000")
#get the website
list_of_elements = driver.find_elements_by_xpath('//a[@href]')
#get all of the items in the content class
print(len(list_of_elements))
#print length - for testing
driver.quit()
for link in list_of_elements:
#iterate through the list
print("iterated!")
print(link.text)
print("printed!")
main()
我的目標是打印此網頁“a”標簽中每個元素的實際鏈接,並嘗試通過打印每個 python href 元素的 .text 形式來實現。 但是無論出於何種原因,它都不起作用。
這是我收到的錯誤:
Traceback (most recent call last):
File "D:\test\test.py", line 26, in <module>
main()
File "D:\test\test.py", line 21, in main
print(link.text)
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webelement.py", line 77, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webelement.py", line 710, in _execute
return self._parent.execute(command, params)
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 423, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\remote_connection.py", line 333, in execute
return self._request(command_info[0], url, body=data)
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\remote_connection.py", line 355, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\urllib3\request.py", line 74, in request
return self.request_encode_url(
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\urllib3\request.py", line 96, in request_encode_url
return self.urlopen(method, url, **extra_kw)
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\urllib3\poolmanager.py", line 376, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen
return self.urlopen(
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen
return self.urlopen(
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen
return self.urlopen(
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\urllib3\connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "C:\Users\bshai1\AppData\Roaming\Python\Python310\site-packages\urllib3\util\retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=56058): Max retries exceeded with url: /session/de3756264b0c42ab0c85d1af17b282d7/element/a45a2689-b7eb-4290-99f6-7b8ea036f14a/text (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000020DF4DFE0E0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
[Finished in 21.9s]
您沒有顯示完整的錯誤消息,也沒有顯示最少的工作代碼——所以我猜。
Selenium 處理對瀏覽器 memory 中對象的引用 - 所以如果您仍想從瀏覽器中獲取某些內容,請不要關閉它。
你必須在for
循環之后使用driver.close()
最小的工作代碼
from selenium import webdriver
def main():
driver = webdriver.Firefox()
driver.get('https://books.toscrape.com')
list_of_elements = driver.find_elements_by_xpath('//a[@href]')
print('len:', len(list_of_elements))
for link in list_of_elements:
print("iterated!")
print(link.text)
print("printed!")
driver.quit() # <--- after `for`-loop
# --- main ---
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.