Carlos Arronte Bello Carlos Arronte Bello - 1 year ago 77
Python Question

return Find_element_by_name as a string In Python

I need some help form all of you, so i have something like this:

element = driver.find_element_by_name("SiteMinderVarForm")

print (element)

When i execute the program i receive:

selenium.webdriver.remote.webelement.WebElement (session="d49d6df9305f2e92eb81aed5c0ed848b", element="0.6436298007036831-4")

And i need a string as result.

I'm trying to automatically log in into a web site, if the process of log in fail by wrong password or wrong user name i need to show a pop up with an error message. So i'm trying to get the name of the form and check if is equal to the actual name. in case if equal that means the process is successful in case not mean the process fail. If you have any other better idea to do that please let me know. Sorry for bad English


Answer Source

From your question, it seems like you want the text value contained in the webelement. You can use the webelement.getAttribute("value") method. If you want to get the name attribute of the webelement with name SiteMinderVarForm:

element = driver.find_element_by_name("SiteMinderVarForm")
value = element.getAttribute("name")

Similar to Using Selenium Web Driver to retrieve value of a HTML input
More examples at: Get Attribute values using Webdriver

You may also be able to use the webelement.getText() method.

element = driver.find_element_by_name("SiteMinderVarForm")
value = element.getText()

Difference between getText() and getAttribute()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download