in page
https://www.virustotal.com/gui/home/upload
a need click "Choose file" but I have Message: javascript error: argument is not defined
var1 = sys.argv[1]
path = os.path.abspath(var1)
driver.get("https://www.virustotal.com/gui/home/upload")
element = driver.execute_script('return document.querySelector("#view-container > home-view").shadowRoot.querySelector("#uploadForm").shadowRoot.querySelector("#infoIcon").shadowRoot.querySelector("#wrapperLink")')
driver.execute_script('argument[0].click();', element)
How to define this element. I would like to send a file to check using the script
CodePudding user response:
Here is something that works.
element = driver.execute_script('return document.querySelector("#view-container > home-view").shadowRoot.querySelector("#uploadForm").shadowRoot')
file_element = element.find_element(By.CSS_SELECTOR, "#fileSelector")
file_element.send_keys(r"your_file_path/filename.extn")
It submits the file from the backend without clicking on the button and opening the file dialog.
Alternate code:
def open_shadow_root(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
driver.get("https://www.virustotal.com/gui/home/upload")
time.sleep(5)
root_1 = driver.find_element(By.CSS_SELECTOR, "#view-container > home-view")
sroot_1 = open_shadow_root(root_1)
print(sroot_1)
time.sleep(4)
root_2 = sroot_1.find_element(By.CSS_SELECTOR, "#uploadForm")
sroot_2 = open_shadow_root(root_2)
time.sleep(4)
file_element = sroot_2.find_element(By.CSS_SELECTOR, "#fileSelector")
file_element.send_keys(r"your_file_path/filename.extn")
time.sleep(4)
driver.quit()
It does the same job as the previous code; it's just that I have written a shadow root function and utilizing it to open the shadow roots. This looks a little more readable in my opinion, but there is no stopping from using the former code.
Also, I have used time.sleep()
which can be replaced with WebdriverWait
if possible.
If you still want to click on the Choose File
button and work through the file dialog, then you may try the below:
element = driver.execute_script('return document.querySelector("#view-container > home-view").shadowRoot.querySelector("#uploadForm").shadowRoot')
choose_file = element.find_element(By.CSS_SELECTOR, "#infoIcon")
choose_file.click()
Alternate:
def open_shadow_root(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
driver.get("https://www.virustotal.com/gui/home/upload")
time.sleep(4)
root_1 = driver.find_element(By.CSS_SELECTOR, "#view-container > home-view")
sroot_1 = open_shadow_root(root_1)
print(sroot_1)
time.sleep(4)
root_2 = sroot_1.find_element(By.CSS_SELECTOR, "#uploadForm")
sroot_2 = open_shadow_root(root_2)
time.sleep(4)
choose_file = sroot_2.find_element(By.CSS_SELECTOR, "#infoIcon")
choose_file.click()
# ... choose the file ....
driver.quit()