Of course! This is an excellent URL for learning about complex selectors because it contains iframes, nested iframes, and a shadow DOM.
It's important to understand that you can't just "get all XPaths" in one go, because elements inside iframes and shadow DOMs exist in different document contexts. An XPath that works on the main page won't find an element inside an iframe.
Below is a breakdown of the key elements on the page, their XPaths, and—most importantly—the context you need to be in to use them.
How to Get an XPath Manually
The simplest way to get an XPath for any element is to use your browser's Developer Tools:
- Right-click on the element you want to inspect.
- Select Inspect.
- The DevTools panel will open with the HTML for that element highlighted.
- Right-click on the highlighted HTML line.
- Go to Copy -> Copy XPath.
XPaths for Elements on the Page
Here are the XPaths for the most important input fields on that page, categorized by their context.
1. Element on the Main Page (No iFrame)
These elements can be accessed directly.
| Element Description | A reliable XPath | Notes |
|---|
| User Name input | //input[@id='user'] | This is a standard element on the main page. No special handling is needed. |
| Password input | //input[@id='pass'] | Also on the main page. |
| Submit button | //input[@type='submit'] | Also on the main page. |
2. Elements Inside the First iFrame
To access these elements, your automation script (like Selenium or Playwright) must first switch its context to the iframe.
- iFrame locator:
//iframe[@id='pact1']
Once you have switched to this iframe, you can use the following XPaths.
| Element Description | A reliable XPath (relative to the iframe) | Notes |
|---|
| First Crush input | //input[@id='glaf'] | This element is inside the pact1 iframe. You must switch to the iframe before you can find this element. |
| Current Crush link | //a[text()='Current Crush'] | This is a <a> tag, not an input, also inside the pact1 iframe. |
Example in Selenium (Python):
# First, switch to the iframe
driver.switch_to.frame("pact1")
# NOW you can find the element inside it
first_crush_input = driver.find_element(By.XPATH, "//input[@id='glaf']")
first_crush_input.send_keys("My first crush")
# To go back to the main page content
driver.switch_to.default_content()
3. Element Inside a Nested iFrame
This is the trickiest part. The "Destiny" input is inside an iframe which is itself inside another iframe.
- Path: Main Page ->
iframe#pact1 -> iframe#pact2
You must switch frames sequentially.
| Element Description | A reliable XPath (relative to the nested iframe) | Notes |
|---|
| Destiny input | //input[@id='jex'] | This is inside iframe#pact2, which is inside iframe#pact1. |
Example in Selenium (Python):
# 1. Switch to the first iframe
driver.switch_to.frame("pact1")
# 2. From within the first iframe, switch to the second iframe
driver.switch_to.frame("pact2")
# 3. NOW you can find the element inside the nested iframe
destiny_input = driver.find_element(By.XPATH, "//input[@id='jex']")
destiny_input.send_keys("Automation")
# To go back to the main page, you must switch back to the default content
driver.switch_to.default_content()
4. Element Inside a Shadow DOM
This is a special case. XPath does not work for elements inside a Shadow DOM. You must use CSS Selectors and access them via JavaScript.
The "Enter pizza name" field is inside a shadow root.
| Element Description | CSS Selector (Not XPath!) | Notes |
|---|
| Enter pizza name | input#pizza | XPath cannot cross the #shadow-root boundary. You must use a CSS selector. |
| The host element | div#shadowcontent | This is the element that "hosts" the shadow DOM. |
Automation requires special handling (e.g., using execute_script in Selenium). The SelectorsHub tool is excellent at generating the correct code for this.
Pro Tip: Use the SelectorsHub Extension
Since you are on the SelectorsHub website, the best tool to use is the SelectorsHub browser extension itself.
- Install the extension in your browser (Chrome, Firefox, Edge, etc.).
- Open the DevTools (F12).
- You will see a "SelectorsHub" tab. Click it.
- Now, when you inspect an element, SelectorsHub will automatically:
- Tell you if it's in an iframe.
- Tell you if it's in a Shadow DOM.
- Provide you with robust, relative XPaths and CSS selectors.
- For iframe elements, it will even give you the command to switch frames.
For example, if you inspect the "Destiny" input with SelectorsHub, it will clearly state that it's in a nested iframe and provide the correct relative XPath.