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

so I want to extract, say value = "TEXT IWANNA EXTRACT; 0" in the html code below. I want to extract the whole string inside the value attribute from "td class =" regu ". But I can't find a way to extract it. I extracted the ppl names, but I cannot extract the string inside the value attribute. Thanks a lot. I have been stuck for 24 hours now. I am open to using other libraries as long as I can check it out.

<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.

      

+3


source to share


2 answers


Once you find the element you want, use the method get_attribute()

:



elm = driver.find_element_by_css_selector("#dbtable input[name=chkStud]")
print(elm.get_attribute("value"))

      

+2


source


table_clas = driver.find_element_by_xpath("//*[@class='dbtable']")
 #select the desired element to thin down the html

td = table_clas.find_elements_by_xpath("//*[@name='chkStud']")

#finally hunt down the element you want specifally.
#find_elements or find_element
#should you use find_elements, then it returns a list you can iterate it
# like

for things in td:
    print things.get_attribute("value") 

      

this prints:



TEXT IWANNA EXTRACT

PLEASE AVOID ME HERE, IM DYING TO GET, 0

-1


source







All Articles