How to locate element without using attributes - Any reliable methods? or What are the best ways to l

16 Replies, 847 Views

Subject: How to locate element without using attributes - Any reliable methods?

Hey folks!

Struggling here... got this element with ZERO attributes (ugh!). Tried the usual XPath/CSS stuff, but nada.

Anyone know how to locate element without using attributes? Like, are there any sneaky ways? Maybe by text content, position, or even JS tricks?

I’ve heard of `contains(text(),'blah')` but feels flaky. Also, is `nth-child` or sibling selectors worth trying? Or am I doomed?

Pls share any hacks or real-world tips. Desperate here!

P.S. If you’ve faced this, how’d u solve it? Thx in advance! 🚀

(Also, sorry for typos—typing too fast lol)
Hey! Been there—super annoying when elements have no attributes. One trick I use is `document.evaluate()` with XPath axes like `following-sibling` or `ancestor`.

Also, Playwright’s `text=` selector is clutch for text-based matching. Example: `page.locator("text='Submit'")`.

If the element’s position is static, `nth-child` can work, but yeah, it’s brittle. Worth a shot though!

Tool rec: Check out [ChroPath](https://autonomiq.io/chropath/) for visualizing XPath/CSS in-browser.
Lol, the struggle is real! For how to locate element without using attributes, I’ve had luck with `querySelector` + `:contains` (jQuery-style) via JS.

Like:
```js
document.querySelector("div:contains('hello')")
```
Not pure CSS, but if you’re automating, Puppeteer/Playwright handle this.

Pro tip: If it’s a button or link, maybe use `getByRole` in Testing Library? Less flaky than text.
Ugh, zero attributes is the worst. For how to locate element without using attributes, try:

- `document.querySelectorAll('*')[index]` (brute-force, but works if DOM is small).
- `elementFromPoint(x,y)` if you know the coords (kinda hacky tho).

Also, `window.getComputedStyle(el)` can sometimes reveal hidden traits.

Tool: [BrowserStack’s Automate](https://www.browserstack.com/automate) lets you test locators cross-browser.
Yo! If you’re dealing with how to locate element without using attributes, XPath axes are your friend. Try `//*[text()='exact text']` or `//*[contains(., 'partial text')]`.

`nth-child` is risky but doable if the DOM never changes. Alternatively, use `parent > child` chains to narrow it down.

For tools, [SelectorGadget](https://selectorgadget.com/) is old but gold for CSS hints.
OP reply:
Whoa, thanks for all the ideas! Tried `text=` in Playwright and it kinda worked, but the element’s text changes sometimes.

Follow-up: Anyone know how to make `contains(text())` more stable? Like, ignoring whitespace or case?

Also, `nth-child` scared me—DOM changes a lot. Maybe I’ll test `getByRole` next.

Y’all saved my sanity today. 🙏
Formal answer: When traditional methods fail for how to locate element without using attributes, consider:

1. Relative XPath (e.g., `//div[@class='parent']/div[2]`).
2. JavaScript’s `querySelector` with `:has` (if supported).
3. Visual locators like Applitools if UI testing.

Resource: [MDN’s XPath guide](https://developer.mozilla.org/en-US/docs/Web/XPath).
Bro, just use `document.getElementsByTagName('*')` and loop through ’em till you find your element.

Kidding… kinda. For real tho, if the element has *any* unique text, `contains()` is your best bet. Flaky? Maybe. But sometimes flaky works.

Also, check if the element has event listeners—might help ID it. DevTools > Elements > Event Listeners tab.
If you’re automating, try `page.$x("//*[contains(., 'your text')]")` in Puppeteer. Works when how to locate element without using attributes is a nightmare.

Or, if the element’s near something identifiable, use `near` in Playwright:
```js
page.locator(':near(:text("Username"), 100px)')
```

Tool: [Playwright docs](https://playwright.dev/docs/locators) have more tricks.



Users browsing this thread: 1 Guest(s)