Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/dotnet/SeleniumDocs/Elements/InformationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public void TestInformationCommands(){
IWebElement emailTxt = driver.FindElement(By.Name("email_input"));
// fetch the value property associated with the textbox
string valueInfo = emailTxt.GetAttribute("value");

// New in Selenium 4.27+
// fetches the DOM attribute exactly as written in the HTML source
string domAttribute = emailTxt.GetDomAttribute("name");

// fetches the live property value from the DOM object (may differ at runtime)
string domProperty = emailTxt.GetDomProperty("name");

Assert.AreEqual(valueInfo, "admin@localhost");

//Quit the driver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ public void informationWithElements() {

// FetchAttributes
// identify the email text box
WebElement emailTxt = driver.findElement(By.name(("email_input")));
WebElement emailTxt = driver.findElement(By.name("email_input"));
// fetch the value property associated with the textbox
String valueInfo = emailTxt.getAttribute("value");

// New in Selenium 4.27+
// fetches the DOM attribute exactly as written in the HTML source
String domAttribute = emailTxt.getDomAttribute("name");

// fetches the live property value from the DOM object (may differ at runtime)
String domProperty = emailTxt.getDomProperty("name");

assertEquals(valueInfo,"admin@localhost");


Expand Down
9 changes: 9 additions & 0 deletions examples/javascript/test/elements/information.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ describe('Element Information Test', function () {

//fetch the attribute "name" associated with the textbox
const nameAttribute = await emailElement.getAttribute("name");

// New in Selenium 4.27+
// fetches the DOM attribute exactly as written in the HTML source
const domAttribute = await emailElement.getDomAttribute("name");
console.log("DOM Attribute:", domAttribute);

// fetches the live property value from the DOM object (may differ at runtime)
const domProperty = await emailElement.getDomProperty("name");
console.log("DOM Property:", domProperty);

assert.equal(nameAttribute, "email_input")
});
Expand Down
8 changes: 8 additions & 0 deletions examples/python/tests/elements/test_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ def test_informarion():
# FetchAttributes
email_txt = driver.find_element(By.NAME, "email_input")
value_info = email_txt.get_attribute("value")

# New in Selenium 4.27+
# fetches the DOM attribute exactly as written in the HTML source
dom_attribute = email_txt.get_dom_attribute("name")

# fetches the live property value from the DOM object (may differ at runtime)
dom_property = email_txt.get_dom_property("name")

assert value_info == "admin@localhost"
Original file line number Diff line number Diff line change
Expand Up @@ -253,31 +253,41 @@ val text = driver.findElement(By.id("justanotherlink")).getText()

## Fetching Attributes or Properties

Fetches the run time value associated with a
DOM attribute. It returns the data associated
with the DOM attribute or property of the element.
To interact with element attributes and properties, Selenium provides specific methods for predictable results. It is important to understand the difference:
* Attribute: The initial value defined in the HTML source code.
* Property: The current value in the browser's DOM, which can be modified by JavaScript or user interaction. For instance, if a user types in an input field, the value property changes.

For this reason, Selenium has two precise methods to get these values: getDomAttribute() and getDomProperty(). The older getAttribute() method is still available for backward compatibility, but its use is discouraged as it can lead to unpredictable results and slower execution.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" text=true >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L60-L64" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L60-L71" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L44-L46" >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L44-L53" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L58-L62" >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L58-L69" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L48">}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L55-L59">}}
{{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L55-L68">}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
// FetchAttributes
// Navigate to URL
driver.get("https://www.selenium.dev/selenium/web/inputs.html")

//fetch the value property associated with the textbox
// fetch the value property associated with the textbox
val attr = driver.findElement(By.name("email_input")).getAttribute("value")

// New in Selenium 4.27+
// fetches the DOM attribute exactly as written in the HTML source
val domAttribute = driver.findElement(By.name("email_input")).getDomAttribute("name")

// fetches the live property value from the DOM object (may differ at runtime)
val domProperty = driver.findElement(By.name("email_input")).getDomProperty("name")
{{< /tab >}}
{{< /tabpane >}}
Loading