简体   繁体   中英

Text on website not scraping using selenium

I am trying to send text from a website that we order tires from into a discord embed. I log into the site, search for an item and see that it is available. I want the discord embed to display the quantity that is available to order, but it just does not display anything. Other elements it is displaying but not the Available , Alt Plant , Tire Tread and Est Delivery does not display.

(Apologies if my code is messy, I am new at this and I have been playing around with it for the last hour)

# Finding the Quantity + Tire Description
    try:
        available1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//*[@id='resultsTable']/tbody/tr[2]/td[9]"))
        )
        altPlant1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/form/div[1]/table/tbody/tr[2]/td[10]"))
        )
        estDelivery1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/form/div[1]/table/tbody/tr[2]/td[11]"))
        )
        tireSize1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/form/div[1]/table/tbody/tr[2]/td[5]"))
        )
        tireTread1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//*[@id='resultsTable']/tbody/tr[2]/td[7]"))
        )
        itemCode1 = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/form/div[1]/table/tbody/tr[2]/td[2]"))
        )
    except:
        driver.quit()
    
    available = available1.text
    altPlant = altPlant1.text
    estDelivery = estDelivery1.text
    tireSize = tireSize1.text
    tireTread = tireTread1.text
    itemCode = itemCode1.text

    print(available)
    print(altPlant)
    print(estDelivery)
    print(tireSize)
    print(tireTread)
    print(itemCode)
    
# Discord Embed Setup   
    embed = Embed(
        description='**Stock available for item number '+pItemNumber+'**',
        color=0x0d0d22,
        timestamp='now'  # sets the timestamp to current time
        )

    embed.set_author(name='Pirelli Stock Check')
    embed.add_field(name='Item Number', value=itemCode, inline=True)
    embed.add_field(name='Tire Description', value=tireTread+' '+tireSize, inline=True)
    embed.add_field(name='Available', value=available+' In Stock', inline=False)
    embed.add_field(name='Alt Plant', value=altPlant+ ' In Stock', inline=True)
    embed.add_field(name='Est Delivery', value=estDelivery+'1', inline=True)
    embed.set_footer(text='Tires Tools', icon_url='https://cdn.discordapp.com/avatars/628005829840470037/8286685de0f2d7d94d94e020caf3265d.png?size=128')

    hook.send(embed=embed)

    print("Embed sent to discord!")

The 4 strings I need are in the source code too. tireTread = CINTURATO P7 , available = 12 , altPlant = 7 and estDelivery = 08/24/2020 .

4 rl

This is what comes through on my discord embed.

在此处输入图像描述

Any idea what I am doing wrong? Let me know if you need more info.

You can find elements by their ID or CSS class:

table = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, "tabla_evolucion")))


table = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.class, "css-class sub-class")))

I recommend you find table by ID then read rows by looping. Maybe you need to change following code to your need.

table_id = self.driver.find_element(By.ID, 'table_id')
rows = table_id.find_elements(By.TAG_NAME, "tr") # get all of the rows in the table
for row in rows:
    # Get the columns (all the column 2)        
    col = row.find_elements(By.TAG_NAME, "td")[1] #note: index start from 0, 1 is col 2
    print col.text #prints text from the element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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