How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python

The First name field within the website https://www.virustotal.com/gui/join-us is located deep within multiple #shadow-root (open).

virustotal_firstname


Solution

To send a character sequence to the First name field you have to use shadowRoot.querySelector() and you can use the following Locator Strategy:

  • Code Block:

    from selenium import webdriver
    import time
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.virustotal.com/gui/join-us")
    time.sleep(7)
    first_name = driver.execute_script("return document.querySelector('vt-virustotal-app').shadowRoot.querySelector('join-us-view.iron-selected').shadowRoot.querySelector('vt-ui-two-column-hero-layout').querySelector('vt-ui-text-input#first_name').shadowRoot.querySelector('input#input')")
    first_name.send_keys("Muhammad Aamir")
    
  • Browser Snapshot:

virustotal_firstname_filled


References

You can find a couple of relevant discussions in:

Leave a Comment