Home > Net >  How to access the html nested within multiple shadowRoot using Selenium and Python
How to access the html nested within multiple shadowRoot using Selenium and Python

Time:01-20

I am trying to build a bot to solve Wordle puzzles on the website (

And this is what I can see on inspection on the website enter image description here

CodePudding user response:

The desired information interms of innerHTML is within multiple #shadow-root (open).

multiple_shadow_root


Solution

To extract the information you need to use shadowRoot.querySelectorAll() and you can use the following Locator Strategy:

  • Code Block:

    driver.get("https://www.powerlanguage.co.uk/wordle/")
    time.sleep(1)
    sends=driver.find_element(By.XPATH, "/html/body")
    sends.click()
    sends.send_keys("adieu")
    sends.send_keys(Keys.ENTER)
    inner_texts = [my_elem.get_attribute("outerHTML") for my_elem in driver.execute_script("""return document.querySelector('game-app').shadowRoot.querySelector('game-row').shadowRoot.querySelectorAll('game-tile[letter]')""")]
    for inner_text in inner_texts:
    print(inner_text)
    
  • Console Output:

    <game-tile letter="a" evaluation="absent" reveal=""></game-tile>
    <game-tile letter="d" evaluation="absent"></game-tile>
    <game-tile letter="i" evaluation="correct"></game-tile>
    <game-tile letter="e" evaluation="absent"></game-tile>
    <game-tile letter="u" evaluation="absent"></game-tile>
    

References

You can find a couple of relevant discussions in:

  •  Tags:  
  • Related