Search code examples
python-3.xselenium-webdriverxpath

Python Selenium Not getting .text Attribute from table


I am having issues scraping the .text attribute from the tag. I am trying to get the quantities in the Quantity column. There are 7 of them as shown below

Text

I am attaching my code below:

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()


Solution

  • Issue in your code was that you were trying to fetch the text without clicking on Pricing element.

    Check the fixed code below:

    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()
    

    Console Result:

    250
    500
    1000
    2500
    5000
    10000
    
    Process finished with exit code 0