Skip to content
The Chat Archive
kimi-k2-0711-preview LMArena Human Preference 140K

Very details xpath scrapping

  1. User

    Very details xpath scrapping

  2. 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:

    1. Top-level document
    2. First iframe (id="pactothefuture")
    3. Nested iframe inside #1 (id="packyak")
    4. 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.


    1. 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

    1. FIRST IFRAME (id="pactothefuture") driver.switch_to.frame("pactothefuture")

    #Stable XPathAbsolute XPath inside iframeNotes
    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)

    1. NESTED IFRAME (id="packyak") driver.switch_to.frame("pactothefuture") driver.switch_to.frame("packyak")

    #Stable XPathAbsolute XPath inside nested iframeNotes
    10//h4[contains(.,'I am an iframe')]/html/body/h4heading
    11//input[@id='tea']/html/body/input“Love Tea”

    1. 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 XPathInner CSS selector (inside shadowRoot)Inner absolute path (shadow DOM)
    12//div[@id='sn_hce']#glafshadow-root/div/input[@id='glaf']
    13same host#jexshadow-root/div/span[@id='jex']

    1. 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.