![]() |
|
[b]"What are the best Selenium locators to find particular text on a webpage?"[/b]
or
[b]"How can I use Selenium l - Printable Version +- Proxy Community (https://proxycommunity.com/forum) +-- Forum: Technical Community Support (https://proxycommunity.com/forum/forum-technical-community-support) +--- Forum: API and Development (https://proxycommunity.com/forum/forum-api-and-development) +--- Thread: [b]"What are the best Selenium locators to find particular text on a webpage?"[/b] or [b]"How can I use Selenium l (/thread-b-what-are-the-best-selenium-locators-to-find-particular-text-on-a-webpage-b-%0A%0Aor-%0A%0A-b-how-can-i-use-selenium-l) Pages:
1
2
|
[b]"What are the best Selenium locators to find particular text on a webpage?"[/b] or [b]"How can I use Selenium l - secureVoyX77 - 06-02-2025 "Struggling to find text on a page with Selenium? What locators work best?" Hey folks! I keep running into issues when trying to use selenium locators to find particular text on a webpage. Like, XPath feels clunky, and CSS selectors don’t always cut it. What’s your go-to method? - XPath with `contains(text(), 'your_text')`? - CSS + `:contains` (if the browser supports it)? - Or just looping through elements and checking `.text`? Kinda tired of trial-and-error here. Any pro tips for using selenium locators to find particular text *efficiently*? Thx in advance! 🚀 *(ps. sorry for typos, typing on mobile lol)* “” - shadowStormX99 - 12-03-2025 XPath with `contains(text(), '...')` is my go-to for selenium locators to find particular text, but yeah, it can get messy. One trick? Use `normalize-space()` to handle extra whitespace or line breaks. Like: `//*[contains(normalize-space(), 'your_text')]` Also, if the site’s dynamic, try waiting for the element *with* the text instead of just the element. Saves a ton of headaches. For tools, check out ChroPath (Chrome extension)—super handy for testing XPaths/CSS live. “” - fastSprint_99 - 15-03-2025 Honestly, looping through elements and checking `.text` is underrated. XPath/CSS can break if the DOM changes even slightly. With Python, something like: ```python elements = driver.find_elements(By.XPATH, "//*") target = [el for el in elements if "your_text" in el.text] ``` Slower? Maybe. But way more reliable for tricky cases where selenium locators to find particular text fail. “” - StealthWebX - 26-03-2025 CSS `:contains` is a lifesaver *if* you’re using jQuery or a browser that supports it. But since most don’t… Try XPath axes! Like `following-sibling::` or `ancestor::` to narrow down where the text lives. Example: `//div[contains(text(), 'hello')]/following-sibling: pan` Also, Browser DevTools (F12) + `Ctrl+F` to test locators before throwing them into Selenium. “” - darkVoyX99 - 29-03-2025 Pro tip: Avoid relying *only* on text for selenium locators to find particular text. Combine it with other attributes! Like: `//button[contains(@class, 'submit') and contains(text(), 'Login')]` Way more stable than just text matching. For debugging, Selenium IDE (Firefox/Chrome) can record actions and show what locators it picks. Not perfect, but helps! “” - deepGlide99 - 01-04-2025 If you’re dealing with dynamic text or i18n, XPath/CSS might not cut it. Try regex in XPath (if your lib supports it): `//*[matches(text(), 'your_regex', 'i')]` Or use JavaScript executor to fetch elements by text: ```js return document.evaluate("//*[contains(text(), 'your_text')]", document, null, XPathResult.ANY_TYPE, null); ``` Clunky, but sometimes the only way. “” - vpnVoyX - 03-04-2025 For me, relative XPaths + text() work best for selenium locators to find particular text. Example: `//div[@id='main']//p[contains(text(), 'lorem')]` But if the text’s split across tags (like `<span>hello</span><span>world</span>`), you’ll need `string()`: `//div[contains(string(), 'hello world')]` Also, Playwright has better text-matching built-in if you’re open to switching tools. “” - secureVoyX77 - 04-04-2025 Wow, didn’t expect so many solid tips! Tried the `normalize-space()` XPath trick—worked like a charm for that whitespace issue I kept hitting. Still struggling with dynamic stuff tho. Gonna test that JS executor approach next. And yeah, maybe time to check out Playwright too lol. Thanks y’all! 🙌 |