In Selenium’s JavaScript bindings, find the element and await its getAttribute() method:
const element = await driver.findElement(By.css('a.example'));
const href = await element.getAttribute('href');
One important detail: Selenium’s getAttribute() can fall back to a same-named DOM property when the HTML attribute is absent. Use getDomAttribute() to read only the attribute, or getProperty() to read the live DOM property. Selenium’s JavaScript WebElement API documents these methods.
Choose the method that matches what you want to read
An HTML attribute is part of an element’s markup, while a DOM property represents the element’s current state in the browser. The values can differ. For example, an input’s initial value attribute may not match the value currently displayed after a user or script changes it.
| Method | What it reads | Fallback behavior | When absent |
|---|---|---|---|
element.getAttribute(name) in Selenium JavaScript |
Attribute value when present | May return a same-named property if the attribute is absent | Returns null if neither is set |
element.getDomAttribute(name) |
HTML/DOM attribute only | No property fallback | Returns null when absent |
element.getProperty(name) |
Current DOM property | Reads the property rather than substituting the attribute | See the property value returned by the browser; do not assume it is the markup attribute |
Native browser element.getAttribute(name) |
DOM attribute value | No Selenium property fallback | Returns null when absent |
Use Selenium’s getDomAttribute() when the test is specifically about markup, getProperty() when it is about the live state, and Selenium’s getAttribute() when its compatibility behavior is what you need. Selenium’s WebElement reference describes the binding methods; the native DOM method’s return behavior is documented by MDN.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
Read an attribute with Selenium’s JavaScript API
Locate the element, then await the result. Selenium’s JavaScript WebElement methods are asynchronous and return promises.
const { Builder, By } = require('selenium-webdriver');
async function readHref() {
const driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('https://example.com');
const link = await driver.findElement(By.css('a.example'));
const href = await link.getAttribute('href');
console.log(href); // string, or null if neither attribute nor property is set
} finally {
await driver.quit();
}
}
readHref();
Replace a.example with a selector that identifies the element on the page you are testing. If you already have a driver, the essential operation is simply:
const element = await driver.findElement(By.css('a.example'));
const value = await element.getAttribute('data-state');
Read only the attribute or only the live property
Attribute only: getDomAttribute()
Use this when the assertion concerns whether the attribute exists in the DOM, without Selenium substituting a same-named property:
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
const hrefAttribute = await element.getDomAttribute('href');
if (hrefAttribute === null) {
console.log('The href attribute is absent');
}
Current state: getProperty()
Use this when you need the live DOM property, which can reflect changes made after the page loaded:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
const currentValue = await element.getProperty('value');
For form controls, this distinction is useful when a test needs to check what is currently entered or selected rather than the initial markup. Do not use a property result as proof of what the original HTML contained.
Run native JavaScript in the browser
If you explicitly want the browser’s native DOM Element.getAttribute(), pass the Selenium element to executeScript(). WebDriver runs the script in the page context and exposes the supplied element as arguments[0]. Selenium’s element-information guide explains JavaScript execution through WebDriver.
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
const hrefAttribute = await driver.executeScript(
'return arguments[0].getAttribute("href")',
element
);
console.log(hrefAttribute); // string, or null if the attribute is absent
This native DOM call returns the attribute’s string value or null when it is absent. Unlike Selenium’s wrapper getAttribute(), it does not fall back to a same-named property; see MDN’s Element.getAttribute() reference.
Handle absent and boolean attributes
Check explicitly for null; an absent lookup is not guaranteed to produce an empty string or false. Selenium documents boolean attributes as returning the string "true" when present and null when absent.
const disabled = await element.getDomAttribute('disabled');
if (disabled !== null) {
console.log('The disabled attribute is present');
}
For native JavaScript, getAttribute() returns the attribute value or null if it is missing. The exact return semantics therefore depend on which API you call, not just on the shared method name.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Troubleshoot common problems
- The value is
null. The attribute may be absent, and Selenium’sgetAttribute()may also have no corresponding property value to return. If absence matters, check=== null; usegetDomAttribute()to test the attribute specifically. - The value differs from the page’s initial HTML. You may be reading a live property or a changed DOM. Use
getDomAttribute()for the current DOM attribute, or inspect the current property withgetProperty()when the test concerns browser state. - The result is a promise rather than the value. Await the Selenium JavaScript method, such as
await element.getAttribute('href'), or handle it with.then(). - The element cannot be found. Check that the selector matches an element in the current page and that navigation or rendering has completed before calling
findElement(). This is a locator or timing issue, separate from attribute retrieval. - A boolean check behaves unexpectedly. Selenium’s documented result for a present boolean attribute is the string
"true", not a JavaScript boolean; the absent result isnull.
Or skip the browser setup
If your goal is to capture a page rather than assert a DOM value, ScreenshotNeo provides a website screenshot API and MCP server. Its one-request endpoint returns an image or PDF; this does not replace Selenium’s DOM attribute methods.
cURL example, following the ScreenshotNeo API documentation:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
ScreenshotNeo accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses include X-Page-Verdict and X-Billed headers. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Frequently Asked Questions
Does Selenium JavaScript getAttribute return a string?
It returns a string when it finds a value and can return null when neither the attribute nor a same-named property is set.
Is Selenium getAttribute the same as JavaScript Element.getAttribute()?
No. Selenium’s WebElement method may fall back to a same-named property; the native DOM method returns the attribute value or null.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




