Python Selenium 未从表获取 .text 属性

问题描述 投票:0回答:1

我在从标签中抓取 .text 属性时遇到问题。我正在尝试获取“数量”列中的数量。如下图有7个

Text

我在下面附上我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC, wait
import time


driver = webdriver.Firefox()


driver.get('https://www.unisourceprint.com/standard-business-cards/')



WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//table/tbody/tr/td[2]')))
quantities = driver.find_elements(By.XPATH,'//table/tbody/tr/td[2]')

for i in quantities:
    print(i.text)

driver.close()

python-3.x selenium-webdriver xpath
1个回答
0
投票

检查下面的固定代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC, wait

driver = webdriver.Firefox()
driver.maximize_window()
driver.get('https://www.unisourceprint.com/standard-business-cards/')

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Pricing']"))).click()
wait.until(EC.presence_of_element_located((By.XPATH, '//table/tbody/tr/td[2]')))
quantities = driver.find_elements(By.XPATH,'//table/tbody/tr/td[2]')

for i in quantities:
    print(i.text)

driver.close()

控制台结果:

250
500
1000
2500
5000
10000

Process finished with exit code 0
© www.soinside.com 2019 - 2024. All rights reserved.