Python Selenium:将结果打印为 [<selenium.webdriver.remote.webelement.WebElement (session="9dc768d9ede71927c1403c99c6a8005b", element="cdb6a")>]

Python Selenium: Printing results as [<selenium.webdriver.remote.webelement.WebElement (session="9dc768d9ede71927c1403c99c6a8005b", element="cdb6a")>]

我有这条线:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from chromedriver_py import binary_path
import config
import pandas as pd
from pretty_html_table import build_table
from mailjet_rest import Client
from apscheduler.schedulers.blocking import BlockingScheduler

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=binary_path, options=chrome_options)
driver.get('https://coinmarketcap.com/new/')

added_ago = driver.find_elements(By.XPATH, '//tbody[1]/tr[1]/td[10]')
print(added_ago)

效果很好,但我不明白发生了什么变化,现在我得到这样奇怪的结果:

[<selenium.webdriver.remote.webelement.WebElement (session="9dc768d9ede71927c1403c99c6a8005b", element="cdb6a60e-d3dc-4f0a-9f6f-afc12b548552")>]

有什么想法吗?

你没看错。

driver.find_elements() returns 元素列表。您正在打印元素本身,所以您看到的可能是列表中唯一的元素:

[<selenium.webdriver.remote.webelement.WebElement (session="9dc768d9ede71927c1403c99c6a8005b", element="cdb6a60e-d3dc-4f0a-9f6f-afc12b548552")>]

理想情况下,您希望在列表中看到 的属性,例如innerText 在这种情况下,您的有效代码块将是:

for element in driver.find_elements(By.XPATH, '//tbody[1]/tr[1]/td[10]'):
    print(element.text)