Lasse
Lasse

Reputation: 141

How to find text from div class Selenium Python

I am trying to get text out of div class statement with Selenium.

from selenium import webdriver
import time
chrome_path = r"C:\Users\User1\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.gigantti.fi/cms/gigantti-outlet/gigantti-outlet/")
time.sleep(10)
posts1 = driver.find_elements_by_class_name('description')
print(posts1)
posts2 = driver.find_elements_by_css_selector('description')
print(posts2)
posts3 = driver.find_elements_by_tag_name('description')
print(posts3)
posts4 = driver.find_elements_by_id('description')
print(posts4)
posts5 = driver.find_elements_by_name('description')
print(posts5)
posts6 = driver.find_elements_by_xpath("//div[@class='description']")
driver.close()

Output is like this

DevTools listening on ws://127.0.0.1:58551/devtools/browser/4ebf2909-a977-43b8-b0bc-2824c2d371d7
[]
[]
[]
[]
[<selenium.webdriver.remote.webelement.WebElement (session="5a662510a9af8bec45752c91b4397d06", element="6e54f7d0-c586-4f97-a974-1af8d19610ce")>]

This it what Chrome shows when inspecting site.

<div class="description">Samsug HD39J1230GW wifi sovitin, erä</div>

I am trying to extract Samsug HD39J1230GW wifi sovitin, erä

Chrome image

Upvotes: 1

Views: 286

Answers (1)

KunduK
KunduK

Reputation: 33384

The elements are available inside iframe Name Gigantti Outlet.You need to switch to iframe first.Try below code.

Induce WebDrivberWait and frame_to_be_available_and_switch_to_it()

Induce WebDrivberWait and visibility_of_all_elements_located()

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

chrome_path = r"C:\Users\User1\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.gigantti.fi/cms/gigantti-outlet/gigantti-outlet/")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"Gigantti Outlet")))
posts1=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,".description")))
for post in posts1:
    print(post.text)

Upvotes: 1

Related Questions