问题
driver.get("https://www.virustotal.com/gui/home/search")
sbox = driver.find_element_by_id("searchInput")
sbox.send_keys("129.226.130.245")
sbox.send_keys(Keys.ENTER)
Please suggest, on how to pass values to search box
回答1:
From what I can see, the element you are trying to search is not an input
element.
You would be interested in INPUT
tag, rather than some div
. So you need to be more specific, something like
driver.findElement( By.xpath( "//div[@id='searchInput']//input" ) )
This syntax may not be correct, as its not tested running program. But you may want to refer this thread in order to get more precise answer. Locating child nodes of WebElements in selenium.
Hope this helps.
回答2:
AFAICT, the website you're trying to automate isn't capable for getting Selenium WebDriver to interact with. It could be from the security/ firewall form its side to detect Selenium Webdriver as the scrape bot or sth... The reason why i'm saying that, because even when i used the Chrome Dev Tools to query from the Console, i was unable to get the element, e.g, getting the Search button on that site:
document.getElementsByClassName('search-button')
then it returned nothing.
I think there's nothing wrong in your scripts but the website itself.
回答3:
The problem here is that you can't find elements that lie within a #shadowroot
.
You can fix this by finding all the 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.
Do the following to access the search input you were looking for:
driver =webdriver.Chrome()
driver.get("https://www.virustotal.com/gui/home/search")
# wait a bit untill search pops up
time.sleep(2)
# Retrieve the last shadowroot using javascript
javascript = """return document
.querySelector('vt-virustotal-app').shadowRoot
.querySelector('home-view').shadowRoot
.querySelector('vt-ui-search-bar').shadowRoot
.querySelector('vt-ui-text-input').shadowRoot"""
shadow_root = driver.execute_script(javascript)
# Find the input box
sbox = shadow_root.find_element_by_id("input")
sbox.send_keys("129.226.130.245")
sbox.send_keys(Keys.ENTER)
回答4:
The search field with placeholder text as URL, IP address, domain, or file hash
within the website https://www.virustotal.com/gui/home/search is located deep within multiple #shadow-root (open)
.
Solution
To send a character sequence to the search 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/home/search") time.sleep(7) search_field = driver.execute_script("return document.querySelector('vt-virustotal-app').shadowRoot.querySelector('vt-auth-checker home-view').shadowRoot.querySelector('vt-ui-search-bar').shadowRoot.querySelector('vt-ui-text-input').shadowRoot.querySelector('input#input')") search_field.send_keys("129.226.130.245")
Browser Snapshot:
References
You can find a couple of relevant discussions in:
- Unable to locate the Sign In element within #shadow-root (open) using Selenium and Python
来源:https://stackoverflow.com/questions/59345959/how-to-pass-the-value-in-search-on-url-https-www-virustotal-com-gui-home-searc