Home > Enterprise >  Store text of driver.find_element into a python variable
Store text of driver.find_element into a python variable

Time:10-27

Im trying to create an discord embed that containts info of some webstite. Im trying to store the driver.find_element.text of with selenium.

Then I want to put that python variable into a json code that makes a discord embed.

The problem is that each product of this page give me 3 different texts. How can I save each one in diferents variables. I put my code here `

from selenium import webdriver
from selenium.webdriver.common.by import By
import csv

DRIVER_PATH = 'C:\chromedriver.exe'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
driver.get('https://www.zalando.es/release-calendar/zapatillas-mujer/')

product_1 = driver.find_element(By.XPATH, '//*[@id="release-calendar"]/div/div[1]/div')
print(product_1.text)

The result in terminakl is 119,95€ adidas Originals Forum MID TM 28 de octubre de 2022, 8:00 Recordármelo

Thanks for the help I really dont know how to save the .text info into differents python varaibles.

CodePudding user response:

Store the text in a variable or a

element_text = product_1.text
element_text_split = element_text.split()  # split by space

If you wanted the price of that item: element_text_split[0] would get the first word

Second word element_text_split[1] is the company

You could also slice up the string using string slicing. Keep in mind not all data you get is going to look exactly the same.

CodePudding user response:

This is what I've used in the past but that's assuming you have a specific element attribute which you're trying to extract:

For example:

element_text = driver.find_element(By.XPATH, '//*[@id="release-calendar"]/div/div[1]/div').get_attribute('title')

You'll need to replace 'title' with the specific attribute from your element which contains the text you want.

  • Related