sam
sam

Reputation: 19164

Get TD element from Table

I am fetching values of debt to equity rations from a webpage.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('--ignore-certificate-errors')
options.page_load_strategy = 'eager'    
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
wait = WebDriverWait(driver, 20)
driver.get("https://www.moneycontrol.com")

inputElement=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL'))) 
inputElement.send_keys('3IINFOTECH',Keys.ENTER)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#proceed-button'))).click()

driver.implicitly_wait(30)
print("---")
try:
    driver.find_element_by_link_text("Financials").click()
    driver.find_element_by_link_text("Ratios").click()
    driver.find_element_by_link_text("Leverage Ratios").click()
    elem = driver.find_element_by_xpath("//*[@id='body']/table//thead/tbody/tr[1]/td[2]")
    print(elem.text)
except e:
    print(e)

I am not able to fetch the Debt to Equity (x) value of latest (Mar 2020) then clicking on Standalone and fetching the same value.

Upvotes: 0

Views: 61

Answers (1)

Swaroop Humane
Swaroop Humane

Reputation: 1836

Please find the working code for the above problem. Enhance the code as per your need.

import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--start-maximized')
options.page_load_strategy = 'eager'
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)
driver.get("https://www.moneycontrol.com")

inputElement = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL')))
inputElement.send_keys('3IINFOTECH', Keys.ENTER)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#proceed-button'))).click()

driver.implicitly_wait(30)
print("---")
try:
    driver.find_element_by_link_text("Financials").click()
    driver.find_element_by_link_text("Ratios").click()
    driver.find_element_by_link_text("Leverage Ratios").click()
    time.sleep(2)
    elem = driver.find_element_by_xpath(
        "//*[@id=\"consolidated\"]/descendant::div[text()=\"Debt to Equity (x)\"]/parent::td/following-sibling::td[1]")
    print("Consolidated : " + elem.text)

    driver.find_element_by_link_text("Standalone").click()
    driver.find_element_by_link_text("Ratios").click()
    driver.find_element_by_link_text("Leverage Ratios").click()
    time.sleep(5)
    elem = driver.find_element_by_xpath(
        "//*[@id=\"standalone\"]/descendant::div[text()=\"Debt to Equity (x)\"]/parent::td/following-sibling::td[1]")
    print("Standalone : " + elem.text)
except:
    print("Error Error Error")

Please mark it as answer if the problem is solved.

Upvotes: 1

Related Questions