简体   繁体   English

HTML 元素未使用 python's.text 打印

[英]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()

My goal here is to print the actual link of each element in the "a" tag of this webpage, and am trying to do it by printing the.text form of each python href element.我的目标是打印此网页“a”标签中每个元素的实际链接,并尝试通过打印每个 python href 元素的 .text 形式来实现。 However it doesn't work for whatever reason.但是无论出于何种原因,它都不起作用。

This is the error that I recieve:这是我收到的错误:

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]

You didn't show FULL error message and you didn't show minimal working code - so I guess.您没有显示完整的错误消息,也没有显示最少的工作代码——所以我猜。

Selenium works on references to objects in browser's memory - so don't close it if you still want to get something from browser. Selenium 处理对浏览器 memory 中对象的引用 - 所以如果您仍想从浏览器中获取某些内容,请不要关闭它。

You have to use driver.close() after for -loop你必须在for循环之后使用driver.close()

Minimal working code最小的工作代码

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM