Как найти поле имени в тени- root (открыть) на веб-сайте https://www.virustotal.com с помощью Selenium и Python - PullRequest
1 голос
/ 12 июля 2020

Я пытаюсь автоматизировать процесс регистрации на сайте вирусов, используя для этого селен в python. Но возникла проблема при получении элемента по идентификатору. Я застрял в этом, любая помощь будет оценена, спасибо. вот мой код, который я пытаюсь.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver =webdriver.Chrome()
driver.get('https://www.virustotal.com/gui/join-us')
print(driver.title)
search = driver.find_element_by_id("first_name")
search.send_keys("Muhammad Aamir")
search.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit()

Ответы [ 2 ]

0 голосов
/ 13 июля 2020

Поле Имя на веб-сайте https://www.virustotal.com/gui/join-us находится внутри нескольких #shadow-root (open).

shadowRoot.querySelector(), и вы можете использовать следующую Стратегию локатора :

  • Блок кода:

    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")
    
  • Снимок браузера:

virustotal_firstname_filled


References

You can find a couple of relevant discussions in:

0 голосов
/ 12 июля 2020

Если вы посмотрите на HTML веб-сайта, вы увидите, что ваше поле ввода находится внутри так называемого #shadowroot.

enter image description here

These shadowroots prevent you from finding the elements contained within the shadowroot using a simple find_element_by_id. You can fix this by finding all the parent shadowroots that contain the element you are looking for. In each of the shadowroots you will need to use javascript's querySelector and find the next shadowroot, until you can access the element you were looking for.

In your case you would need to do the following:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver =webdriver.Chrome()
driver.get('https://www.virustotal.com/gui/join-us')
print(driver.title)

# wait a bit untill form pops up
time.sleep(3)

# Retrieve the last shadowroot using javascript
javascript = """return document
.querySelector('vt-virustotal-app').shadowRoot
.querySelector('join-us-view').shadowRoot
.querySelector('vt-ui-text-input').shadowRoot"""
shadow_root = driver.execute_script(javascript)


# Find the input box
search = shadow_root.find_element_by_id("input")
search.send_keys("Muhammad Aamir")
search.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit()

...