Victor Mirault
Victor Mirault

Reputation: 17

Python variable webelement/list to .txt Selenium

I would like to know the function to transform a list / webelement variable into .txt with the code below I can only read the file on my console en.txt but not save it on my pc.

recuperation_nom = driver.find_elements_by_class_name("title-3")

for resultat_nom in recuperation_nom:
    print(resultat_nom.text)

Upvotes: 0

Views: 172

Answers (1)

Rustam Garayev
Rustam Garayev

Reputation: 2692

Just open .txt file in access mode 'a', then write each line into it.

recuperation_nom = driver.find_elements_by_class_name("title-3")

with open('output.txt', 'a') as f:
    f.truncate(0)
    for resultat_nom in recuperation_nom:
        f.write(f"{resultat_nom.text}\n")

Upvotes: 1

Related Questions