Troubleshoot "Name" from a web page
How can I get the "Name" on the landing page in a script. I tried as below, but it throws the following error:
"selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//div[@class="div_input_place"]/input[@id="txt_name"]/@value" is: [object Attr]. It should be an element."
However, the element that contains "Name" after:
<div class="div_input_place">
<input name="txt_name" type="text" value="CLINTO KUNJACHAN" maxlength="20" id="txt_name" disabled="disabled" tabindex="2" class="aspNetDisabled textboxDefault_de_active_student">
</div>
script I have tried so far:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.icaionlineregistration.org/StudentRegistrationForCaNo.aspx")
driver.find_element_by_id('txtRegistNo').send_keys('SRO0394294')
driver.find_element_by_id('btnProceed').click()
time.sleep(5)
name = driver.find_element_by_xpath('//div[@class="div_input_place"]/input[@id="txt_name"]/@value')
print(name.text)
driver.quit()
+3
source to share
2 answers
Selenium
does not support this syntax. Your expression XPath
should only return WebElement
, but not the attribute value or text. Try using below code instead:
name = driver.find_element_by_xpath('//div[@class="div_input_place"]/input[@id="txt_name"]').get_attribute('value')
print(name)
+3
source to share
You cannot target attributes with XPaths in Selenium - expressions must always match actual elements:
name_element = driver.find_element_by_xpath('//div[@class="div_input_place"]/input[@id="txt_name"]')
name_attribute = name_element.get_attribute("value")
print(name_attribute)
Note that I also switched to a more concise and readable CSS selector:
driver.find_element_by_css_selector('.div_input_place input#txt_name')
Or, even go with "find by id" if your id is unique:
driver.find_element_by_id("txt_name")
+3
source to share