Using selenium to find values from a dynamic webpage table

Viewed 112

I have a python code which uses selenium to open a College results web page, input some student values and open the results page of that student. The results page has a dynamic table whose HTML code I'm not able to access. How do I find values of a specific row and column ?

My python code to open the College results web page is :

from selenium.webdriver.support.ui import Select
firefox_browser = webdriver.Firefox(executable_path=r'C:\Program Files\gecko\geckodriver.exe')
firefox_browser.get("http://results.drait.in/")
time.sleep(5)
print("Opening firefox")
select = Select(firefox_browser.find_element_by_id('ugpg'))
select.select_by_visible_text('UG-SEE')
name_input = firefox_browser.find_element_by_css_selector("#usn")
play_button = firefox_browser.find_element_by_css_selector("#submit")
name_input.send_keys("1DA17ISxxx")
play_button.send_keys(Keys.ENTER)
print("Done")

The above code opens up the main results page which has the subjects the student has enrolled for and the respective grades.

How can I access a particular row and column based on the subject code and exam type and return that value to my python code to print ?

I am not able to find the HTML code of the results page in order to access the table using the table name.

The homepage of the results page looks like this : https://imgur.com/a/1ZgENY6

The result page with the subject details and grades after providing student ID looks like this : https://imgur.com/a/xDAJ7IL

Thanks

1 Answers

First of all you have to import selenium webdriver

from selenium import webdriver

Then you have to import Keys to enable enter key

from selenium.webdriver.common.keys import Keys

Then if you want to use time.sleep() you have to import time module

import time

Inspect the table you can take xpath of all <tr> tags present in the table tag

your xpath will look like this /html/body/form/center/table/tbody/tr[1]

from this remove index values /html/body/form/center/table/tbody/tr

using len method you can see how many rows are the

a = len(firefox_browser.find_elements_by_xpath("/html/body/form/center/table/tbody/tr"))

similarly use xpath of <th> tags for columns

your xpath will look like this /html/body/form/center/table/tbody/tr[1]/th[1]

from this remove index values of <th> tag /html/body/form/center/table/tbody/tr[1]/th

using len method you can see how many colums are the

b = len(firefox_browser.find_elements_by_xpath("/html/body/form/center/table/tbody/tr[1]/th"))

now a represents how many rows are there and b represents the no of columns

Write a for loop

for r in range(2,a+1):#starting from 2 because first rows is headings
   for c in range(1,b+1):
        #use the variables a and b instead of the index of <tr> and <th>
        
         value = 
firefox_browser.find_elements_by_xpath("/html/body/form/center/table/tbody/tr["+r+"]/th["+c+"]")
         print(value.text)

If you want use if statements to pick elements from particular heading

use like this

check=firefox_browser.find_elements_by_xpath("/html/body/form/center/table/tbody/tr[1]/th[1]")

if check.text == "your value":
       #do something
Related