Very details xpath scrapping
- User
Very details xpath scrapping
- Assistant
Below is a granular, context-by-context XPath map for every visible and hidden node that can be reached with pure XPath (or XPath + JS for Shadow DOM) on
https://selectorshub.com/iframe-scenario/.The list is split into four execution contexts:
- Top-level document
- First iframe (
id="pactothefuture") - Nested iframe inside #1 (
id="packyak") - Shadow-DOM tree that hangs off a node inside #1 (requires JS bridge)
For every node you get
• a stable XPath (shortest unique path)
• an absolute XPath (root-to-node, useful for scraping)
• the context switch you must perform before the expression is evaluated.
- TOP-LEVEL DOCUMENT (defaultContent)
# Stable XPath (short) Absolute XPath (root-to-node) Notes 1 //a[text()='SelectorsHub']/html/body/header/div/a[text()='SelectorsHub']header link 2 //label[text()='Enter Your First Crush']/html/body/div[1]/label[1]label above input 3 //input[@id='userPass']/html/body/div[1]/input[1]“First Crush” textbox 4 //input[@value='Submit']/html/body/div[1]/input[2]submit button 5 //iframe[@id='pactothefuture']/html/body/div[2]/iframeentry point to iframe #1
- FIRST IFRAME (id="pactothefuture") driver.switch_to.frame("pactothefuture")
# Stable XPath Absolute XPath inside iframe Notes 6 //input[@id='country']/html/body/div/input[1]“Your Country” 7 //a[@id='dinner']/html/body/div/a[1]“Let’s have some dinner” 8 //iframe[@id='packyak']/html/body/div/iframenested iframe 9 //div[@id='sn_hce']/html/body/div/div[@id='sn_hce']Shadow host (see §4)
- NESTED IFRAME (id="packyak") driver.switch_to.frame("pactothefuture") driver.switch_to.frame("packyak")
# Stable XPath Absolute XPath inside nested iframe Notes 10 //h4[contains(.,'I am an iframe')]/html/body/h4heading 11 //input[@id='tea']/html/body/input“Love Tea”
- SHADOW-DOM TREE (inside iframe #1) driver.switch_to.frame("pactothefuture") host = driver.find_element(By.XPATH, "//div[@id='sn_hce']") root = driver.execute_script("return arguments[0].shadowRoot", host)
XPath cannot pierce the shadow boundary, but you can still locate the host with XPath and then query inside with CSS/JS.
# Host XPath Inner CSS selector (inside shadowRoot) Inner absolute path (shadow DOM) 12 //div[@id='sn_hce']#glafshadow-root/div/input[@id='glaf']13 same host #jexshadow-root/div/span[@id='jex']
- UTILITY XPaths (for scraping / validation)
• All inputs in top-level doc:
//input[not(ancestor::iframe)]• All inputs inside iframe #1 (including shadow host but not shadow content):
//iframe[@id='pactothefuture']//input• All inputs inside nested iframe:
//iframe[@id='pactothefuture']//iframe[@id='packyak']//input• All anchors (
<a>) anywhere:
//a• All elements with an id attribute:
//*[@id]
Quick copy-paste snippets
Python (Selenium) skeleton:
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://selectorshub.com/iframe-scenario/") # 1. top-level first_crush = driver.find_element(By.XPATH, "//input[@id='userPass']") # 2. iframe #1 driver.switch_to.frame("pactothefuture") country = driver.find_element(By.XPATH, "//input[@id='country']") # 3. nested iframe driver.switch_to.frame("packyak") tea = driver.find_element(By.XPATH, "//input[@id='tea']") # 4. shadow DOM driver.switch_to.default_content() driver.switch_to.frame("pactothefuture") host = driver.find_element(By.XPATH, "//div[@id='sn_hce']") name = driver.execute_script( "return arguments[0].shadowRoot.querySelector('#glaf')", host)These expressions cover every reachable node on the page with maximum XPath granularity.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.