Obtaining table data and HTML elements using the correct code - Selenium and Python

I've been attempting to extract the ticker symbol, stock name, price, sector, and market cap columns from this website. I'm facing challenges in identifying the correct HTML elements using the appropriate code. Although I've used Selector Gadget to locate the XPaths, my confidence in understanding HTML trees and rules isn't very high. Notably, the first three columns appear to be grouped within a single td on the webpage. Below is the current code snippet which pulls entire rows. Any assistance would be greatly appreciated.

from selenium import webdriver
from selenium.webdriver.common.by import By
import re
import pandas as pd

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
website = 'https://www.tradingview.com/markets/stocks-turkey/market-movers-all-stocks/'
driver.get(website) 

while True:
    try:
        loadMoreButton = driver.find_element(By.XPATH,'//*[contains(concat( " ", @class, " " ), concat( " ", "content-D4RPB3ZC", " " ))]')
        time.sleep(2)
        loadMoreButton.click()
        time.sleep(5)
    except Exception as e:
        print (e)
        break
print ("Complete")
time.sleep(10)

matches = driver.find_elements(By.TAG_NAME,'tr')

ticker_symbol = []
ticker_name = []
ticker_price =[]
ticker_sector =[]
ticker_marketcap =[]

for match in matches:
    print(match.text)

driver.quit()

Answer №1

I made several improvements to the code

  1. Replaced instances of .sleep() with appropriate use of WebDriverWait
  2. Updated locators for better accuracy

Below is the updated and functional code.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

url = 'https://www.tradingview.com/markets/stocks-turkey/market-movers-all-stocks/'
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)

while True:
    try:
        driver.find_element(By.XPATH,'//span[text()="Load More"]').click()
    except StaleElementReferenceException:
        break

wait = WebDriverWait(driver, 10)
rows = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,'table[class="table-Ngq2xrcG"] tr.listRow')))
for row in rows:
    ticker_symbol = row.find_element(By.XPATH, './td[1]//a').text
    ticker_name = row.find_element(By.XPATH, './td[1]//sup').text
    ticker_price = row.find_element(By.XPATH, './td[2]').text
    ticker_marketcap = row.find_element(By.XPATH, './td[6]').text
    try:
      ticker_sector = row.find_element(By.XPATH, './td[11]/a').text
    except NoSuchElementException:
      ticker_sector = "—"

    print(ticker_symbol, ticker_name, ticker_price, ticker_marketcap, ticker_sector)

driver.quit()

Here is an example of the output:

A1CAP A1 CAPITAL YATIRIM 24.76 TRY 3.38B TRY Finance
ACSEL ACIPAYAM SELULOZ 99.7 TRY 1.104B TRY Process Industries
ADEL ADEL KALEMCILIK 322.50 TRY 7.69B TRY Consumer Durables
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered a <Errno 32 broken pipe> error when attempting to upload a large file using

When attempting to upload a large file (exceeding 1GB) using a remote selenium server, an error occurs with the message urllib.error.URLError <urlopen error [Errno 32] Broken pipe>. Below is a snippet of python code that includes setup for a remote ...

View does not display initial value selected in ReactJS

I'm having trouble setting an initial value for a select element that isn't displaying in the view. Here's my JSX: var SelectOKNO = React.createClass({ render() { return ( <div> <select className="selectpicker" ...

What is the Angular alternative to control.disable when working with a QueryList?

I have encountered a challenge in my Angular form where I need to disable certain fields. The issue arises from the fact that many of the HTML tags used are customized, making it difficult to simply use the disabled attribute. To address this, I have opted ...

Selenium encountered an error: Element not found - Could not locate element: {"method":"CSS selector","selector":"*[data-test="email-input"]}

Recently, my application has been encountering an error when trying to log in through a web form. The issue seems to be with locating the email input field: "no such element: Unable to locate element: {"method": "css selector", "s ...

When using NextJS, the dynamically generated HTML elements do not get affected by CSS repaint

Recently, I encountered an issue while working on a Next.js project involving the addition of an external script that uses vanilla JavaScript to dynamically create nodes. Despite importing global CSS at the top of my _app.js file, I noticed that the styles ...

html form shifting positions based on screen resolution

I have been experimenting with a login screen design for my website recently. I created a form with a fixed position, but as the screen resolution changes, not only does the form's position shift, but also the image moves, causing an unwanted interse ...

Issue: "TypeError: 'module' object is not callable" encountered while attempting to visualize a decision tree for a machine learning model

After writing the code to visualize the decision tree model, I initially faced errors such as 'graphviz's executables not found'. However, I managed to resolve this issue by adding the path to the environment variables and re-installing the ...

Having issues displaying the & symbol in HTML from backend data

I recently worked on a project using express handlebars, where I was fetching data from the YouTube API. However, the titles of the data contained special characters such as '# (the ' symbol) and & (the & symbol). When attempting to render the ...

What happens when there is no match found while attempting to edit a form with the UI-Bootstrap typeahead directive?

Here is the code that demonstrates how my typeahead input box functions. Users can enter a name, and the relevant information will populate the rest of the form. If no match is found, users should still be able to input the name and related details into th ...

Strategies for effectively managing and validating Mongoengine objects containing ReferenceFields

Hey there! I'm currently working on developing a REST API using Flask-RESTful, MongoEngine, and marshmallow. I've run into some challenges when it comes to testing my models that contain ReferenceFields. Specifically, I have a model called "Pra ...

Is it possible to implement multiprocessing with a for loop?

With an array named data_inputs containing the names of numerous astronomy image files which are then manipulated in my code, each image takes a few seconds to process. The issue is that only one image can be processed at a time due to running the array th ...

Obtaining Google AAID Using a Mobile Browser (JavaScript)

I've been looking for information online without much success regarding this topic. I am interested in retrieving a user's Google Advertising ID (AAID) through my website when they visit using a mobile browser. I assume it could be done with som ...

What is the best way to include a class with Knockout JS?

After going through the basic Knockout tutorial and examining various examples, I decided to try it out myself on jsFiddle. However, I encountered some issues as my attempts did not quite work. The goal is simple - I just want to add the class open to a d ...

Which is better for a web automation bot in Python: Multithreading or Multiprocessing?

I have a script that continuously monitors the prices of specific products on nike.com. Once the price drops, it will automatically initiate multiple instances to log in and purchase the item from different accounts. While I already have the scraping and ...

Positioning Firefox absolutely within a table-cell element is possible

Interestingly, it's not Internet Explorer causing me trouble this time, but Firefox. Here is the Fiddle link for reference: http://jsfiddle.net/EwUnt/ The purpose of this table is to highlight both the row and column of the cell where the cursor is ...

Can you specify the doctype used for XHTML5, which is the XML representation of HTML5?

Which doctype is recommended for XML serialization of HTML5? If I keep the file extension as .html, can I still indicate to the browser that the content is XHTML5? ...

A Pydantic list that always includes two distinct types of dictionaries, but never the same one twice

I'm struggling with a persistent issue. Here is an example of JSON data: {"sourceInfo": { "list": [ { "Ec2AssetSourceSimple": { "instanceType": "t2.micro", ...

Avoiding data in DataFrame from being replaced by csv file?

I have a python script that keeps overwriting the file with new data each time it runs. Can someone please advise me on how to prevent this from happening? Here's an example of what is currently happening: DF1 Table Count case ...

Starting a selenium-based standalone executable without the need for Chrome to be installed

Recently, I successfully created a Python script utilizing Selenium and a Chromedriver webdriver. I then used cx_freeze to convert my script into an executable (.exe) file for easy execution by double-clicking. However, the downside is that the script reli ...

Transferring a Variable from Arduino to JavaScript

Is there a simple way to pass an Arduino variable as a JS variable? Let's say we have an integer Arduino variable called sensorData: int sensorData = analogRead(sensorPin); How can we transfer this value to a JavaScript variable? client.println(&quo ...