user18370758
user18370758

Reputation:

How print as text an Selenium html element?

I'm making an Selenium project for fun. I want see all football score on my terminal. I use Selenium for scraping. But I cannot print the scraped element. How can I fix that?

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import os
from bs4 import BeautifulSoup
import lxml


team_both = []
team_one = []
team_two = []
team_id = []

driver = webdriver.Chrome(ChromeDriverManager().install())
time.sleep(1)
#os.system('clear')
time.sleep(1)
driver.get('https://www.bet365.de/#/IP/B1')

time.sleep(1)

try:
    driver.find_element_by_class_name('iip-IntroductoryPopup_Cross').click()
except:
    pass

time.sleep(1)

# Scroll to bottom
element = driver.find_element_by_class_name("ovm-OverviewScroller-enabled")
actions = ActionChains(driver)
actions.move_to_element(element).click_and_hold().perform()

soup = BeautifulSoup(driver.page_source, 'lxml')

time.sleep(5)
os.system('clear')
#ovm-FixtureDetailsTwoWay_TeamName 
teams = soup.find_all('div' , {"class" : "ovm-FixtureDetailsTwoWay_TeamName "})
for i in teams:
    print(i.text)

Upvotes: 1

Views: 609

Answers (1)

Prophet
Prophet

Reputation: 33361

The simplest way to extract a text from web element is by applying the .text method, but since you are using find_all method, the team_both is not a single web element rather a list of web elements.
In order to get texts from the web elements in a list you have to iterate over the elements in the list and extract text from each element, as following:

team_both = soup.find_all('div', {"class" : "ovm-FixtureDetailsTwoWay_TeamName"})
for team in team_both:
    print(team.text)

Upvotes: 0

Related Questions