Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the text of value attribute indside a tag using selenium

so I wanna extract lets say value="THE TEXT IWANNA EXTRACT ;0" at the html code below. I wanna extract all the string inside value attribute of "td class="regu". But I cant seem to find a way to extract it. I have extracted Names of ppl but I cant extract the string inside value attrib. Any help is much appreciated. Thankyou. Im stuck for like 24 hours already. Im open to use other Libraries as long as I can extract it.

<table class="dbtable" border="0" width="100%">
                           <tbody><tr>
                             <td class="tableheader" align="center" width="1%"><b>#</b></td>
                                       <td class="tableheader" align="center" width="60%"><b>User Name</b></td>
                                       <td class="tableheader" align="center"><b>User Type</b></td>
                           </tr><tr bgcolor="#ffffff">
                         <td class="regu"><input name="chkStud" value="THE TEXT IWANNA EXTRACT ;0" type="checkbox"></td>
                         <td class="regu">NAME OF STUDENT HERE   </td>
                         <td class="regu">&nbsp;Student</td>
                       </tr><tr bgcolor="#ffffff">
                         <td class="regu"><input name="chkStud" value="PLEASE EXTRACT ME HERE, IM DYING TO GET OUT;0" type="checkbox"></td>
                         <td class="regu">FOO BAR FOO BAR</td>
                         <td class="regu">&nbsp;Student</td>
                         </tbody></table>

Here is the python code

#!/usr/bin/python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


from bs4 import BeautifulSoup
import logging
driver = webdriver.Firefox()
driver.get("http://somewebsite/iwannascrape/login.php") #page requires a login T_T
assert "Student" in driver.title
elem = driver.find_element_by_name("txtUser")
elem.clear()
elem.send_keys("YOU_SIR_NAME") #login creds. please dont mind :D 
elem2 = driver.find_element_by_name("txtPwd")
elem2.clear()
elem2.send_keys("PASSSWORDHERE")  
elem2.send_keys(Keys.RETURN)
driver.get("http://somewebsite/iwannascrape/afterlogin/illhere")






# using this to extract only the table with class='dbtable' so its easier to manipulate :)
table_clas = driver.find_element_by_xpath("//*[@class='dbtable']")



source_code = table_clas.get_attribute("outerHTML") #this prints out     the     table and its children.
print source_code



for i in range (10): #  spacing for readability
    print "\n"



print table_clas.text #this prints out the names.
like image 994
Skrmnghrd Avatar asked Dec 04 '25 04:12

Skrmnghrd


1 Answers

Once you locate the desired element, use get_attribute() method:

elm = driver.find_element_by_css_selector("#dbtable input[name=chkStud]")
print(elm.get_attribute("value"))
like image 137
alecxe Avatar answered Dec 05 '25 21:12

alecxe