我在从标签中抓取 .text 属性时遇到问题。我正在尝试获取“数量”列中的数量。如下图有7个
我在下面附上我的代码:
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()
检查下面的固定代码:
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