1

I have wrote the code

import os
from webdriver_manager.chrome import ChromeDriverManager
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)
url = "https://www.moneycontrol.com/india/stockpricequote/chemicals/tatachemicals/TC"
driver.get(url)
try:
    wait = WebDriverWait(driver, 10)
except Exception:
driver.send_keys(Keys.CONTROL +'Escape')
driver.find_element_by_link_text("Bonus").click()
try:
    wait = WebDriverWait(driver, 5)
except Exception:
    driver.send_keys(Keys.CONTROL +'Escape')
for i in range(0, 50):
    bonus_month = driver.find_element_by_xpath ("//*[@class= 'mctable1.thborder.frtab']/tbody/tr[%s]/td[1]"%(i)) 
    print(bonus_month.text)
    bonus = driver.find_element_by_xpath ("//*[@class= 'mctable1.thborder.frtab']/tbody/tr[%s]/td[1]"%(i)) 
    print(bonus.text)

This gives me error

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class= 'mctable1.thborder.frtab']/tbody/tr[0]/td[1]"}

Element on the page: enter image description here

Where I am making mistake in finding Exbonus and Ratio?

4
  • bonus_month - which element on page is it? Commented Apr 18, 2021 at 5:36
  • There is error because this element cannot be found Commented Apr 18, 2021 at 5:44
  • but I have added driver.find_element_by_link_text("Bonus").click() to click on Bonus link and page is opening ahead but not selecting the data. Commented Apr 18, 2021 at 6:19
  • Does it make a click? Commented Apr 18, 2021 at 6:20

2 Answers 2

1

First use the clickable method from the expected conditions to check that the element is clickable within given time to just make sure it is operational.

Once the click action performed on the bonus link the table takes some time to finishes loading. In meantime selenium tries to fetch the table content and fails to get it. So again add wait for the element to load and then grab the table using Xpath and iterate over the rows of the table. -

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@id='corporation_tab']//a[@href='#ca_bonus']")))

driver.find_element_by_link_text("Bonus").click()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//tbody[@id='id_b']/tr")))

tableRows = driver.find_elements_by_xpath('//tbody[@id="id_b"]/tr')
print(tableRows)
for i in tableRows:
    AnnouncementDate = i.find_element_by_xpath('td[1]').text
    exbonus = i.find_element_by_xpath('td[2]').text
    ratio = i.find_element_by_xpath('td[3]').text

    print(AnnouncementDate + " \t\t " + exbonus + " \t\t " + ratio)

This returns me the output -

enter image description here

You will need following extra import -

from selenium.webdriver.support import expected_conditions as EC
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, already reached to this point but how can we separate dates and bonus?
@sam Updated the answer as per you requirement. It was easy though.
1

This partially will solve your issue with locators:

1 To find Ex-Bonus use css selector: #id_b>tr>td:nth-of-type(2)

2 To find ratio use also css selector, #id_b>tr>td:nth-of-type(3)

To iterante use:

#id_b>tr:nth-of-type(x)>td:nth-of-type(3)

where x is the number of row. For example, #id_b>tr:nth-of-type(1)>td:nth-of-type(3) will give you text with ratio 3:5

enter image description here

If you avoid avoid using #id_b, this locator will not be unique. Instead of range function I'd use find_elements_by_css_selector. Try following:

rows = driver.find_elements_by_css_selector("#id_b>tr")
for row in rows:
    bonus = row.find_element_by_css_selector("td:nth-of-type(2)").text
    ratio = row.find_element_by_css_selector("td:nth-of-type(3)").text

There are only 5 of this elements on the page. I'll have to click See More.

14 Comments

This bonus and ratio prints nothing but blank lines.
row.text gives "19 Jun, 1995 04 Oct, 1995 3:5" but bonus and ratio print blank
why we cant locate using table class and then tr td?
AttributeError: 'WebElement' object has no attribute 'driver'
Yes, I removed this part, check again
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.