cookiemnstr247
cookiemnstr247

Reputation: 131

Python Selenium Read Values from xPath attributes

I am preparing a program that reads values from a time sheet. I am using python 3 and selenium using Chrome webdriver.

I have been able to use the .text method to capture the text values in the first three columns (Time Entry,Category, and Totals). However, using the same method does not work on the input boxes starting in column 4 through to.

enter image description here

Here is the xpath for the first row, fourth column.

/html/body/form/table/tbody/tr/td/table/tbody/tr[2]/td[2]/table[3]/tbody/tr/td[2]/div/table[1]/tbody/tr/td/table[1]/tbody/tr[1]/td[1]/input

Here is the html for the first row, fourth column (with value 7)

<input name="grdDaysProjects$ctl02$txtPrjCol0" type="text" value="7" maxlength="5" readonly="readonly" id="grdDaysProjects_ctl02_txtPrjCol0" class="GridInputsShort" onblur="GetTotalRow('1','grdComboProjects_ctl02_lblHours',2,6,'grdDaysProjects'); VerticalSum(0,'grdTotal',2,1,0); AntiXSS('grdDaysProjects_ctl02_txtPrjCol0')" data-origvalue="7">

Here is the html for the second row, fourth column (with no value)

<input name="grdDaysProjects$ctl03$txtPrjCol0" type="text" maxlength="5" readonly="readonly" id="grdDaysProjects_ctl03_txtPrjCol0" class="GridInputsShort" onblur="GetTotalRow('1','grdComboProjects_ctl03_lblHours',3,6,'grdDaysProjects'); VerticalSum(0,'grdTotal',2,1,0); AntiXSS('grdDaysProjects_ctl03_txtPrjCol0')" data-origvalue="">

It should extract a text value of 7 while any blank cells should extract a zero or null value. Using the .text method with the find_element_by_xpath method results in only null values. The html has the value in two places, the "value" and "data-origvalue". When the value is null, the "value" variable does not appear in the html but the "data-origvalue" does. Any suggestions on getting the value from the "data-origvalue" using selenium?

Upvotes: 1

Views: 78

Answers (1)

KunduK
KunduK

Reputation: 33384

Input tag having attribute value Try to fetch that value using element.get_attribute("value")

element=driver.find_element_by_xpath("your xpath")
print(element.get_attribute("value"))

Upvotes: 1

Related Questions