Skip to content Skip to sidebar Skip to footer

Python Selenium Store Data To Specific Column In CSV?

I have two prints that I want to write to a single CSV File into Column A and Column B My problem is when I print both(first and second print) at the end , I get only an element,

Solution 1:

when you write driver.switch_to.frame(i) you are basically accessing iframe html element. like normal html page you can access its inside element as well.

from your previous question iframe was like

<body>
<a href="http://www.test2.com" target="_blank">
<img src="https://img2.test2.com/LWBAD-1.jpg"></a>
<br/>Code: LWBAD

you can easily access image url by

img_src = driver.find_element_by_tag_name('img').get_attribute('src')

and store that in csv file

code:

from bs4 import BeautifulSoup
from selenium import webdriver
import html5lib
import time
import requests
import csv

driver_path = '/usr/local/bin/chromedriver 2'
driver = webdriver.Chrome(driver_path)
driver.implicitly_wait(10)

driver.get('https://www.example.com')

iframe = driver.find_elements_by_tag_name('iframe')
images = driver.find_elements_by_tag_name('img')
with open('file_name.csv', 'w', newline='') as csvfile:
    field_names = ['text', 'src']
    writer = csv.DictWriter(csvfile, fieldnames=field_names)
    writer.writerow({'text': 'text', 'src': 'src'})
    for i in range(0, len(iframe)):
        f = driver.find_elements_by_tag_name('iframe')[i]
        img_src = images[i].get_attribute('src')

        # do the src splitting here
        img_src = img_src.split('=')[1]

        driver.switch_to.frame(i)

        text = driver.find_element_by_tag_name('body').text


        text = text.replace("Code: ", "")
        text = text.replace("No Copy Images to TW Server", "")
        print(text)
        writer.writerow({'text': text, 'src': img_src})

        driver.switch_to_default_content()
driver.quit()

Post a Comment for "Python Selenium Store Data To Specific Column In CSV?"