gpt4 book ai didi

Python Selenium 通过 Windows 上传上传文件

转载 作者:行者123 更新时间:2023-12-03 20:17:44 24 4
gpt4 key购买 nike

我阅读了很多关于并且每个人都建议不要使用 Windows 上传并直接写入我的文件路径的建议;我什至尝试使用在论坛中找到的一些命令,例如:

swicthTo()
switch_to_window()
window_handles

但我还没有找到任何解决方案。
我的主要问题是当时我没有空间直接发送我的文件路径(见下图,介绍路径的空间是灰色的)但我只能选择单击“浏览”并打开 Windows上传者:

enter image description here

你知道如何切换到上传窗口的Windows并介绍我的文件吗?

我什至以这种方式尝试:
browse=wait(".//*[@id='fileinput']") #open the window upload
browse.click()
time.sleep(1)


def handle_dialog(element_initiating_dialog, dialog_text_input):
upload_dialog = driver.switch_to_active_element
print (upload_dialog)
upload_dialog.send_keys(dialog_text_input)
upload_dialog.send_keys(selenium.webdriver.common.keys.Keys.ENTER) # the ENTER key closes the upload dialog, other thread exits

handle_dialog(browse, "foobar.txt")

我找到了 window ,当我打印时,我有这个对象:
We are already in the Location->Details Page <bound method WebDriver.switch_to_active_element of <selenium.webdriver.ie.webdriver.WebDriver (session="3e725bb7-40a7-452a-ad90-9cca1d41296a")>>

但是当我尝试执行 send_keys 后,我收到此错误:

Traceback (most recent call last): File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 149, in handle_dialog(browse, "foobar.txt") File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 145, in handle_dialog upload_dialog.send_keys(dialog_text_input) AttributeError: 'function' object has no attribute 'send_keys'



我部分找到了一个解决方案,我让我的代码表现得好像窗口上传是一个警报,所以我这样做:
browse=wait(".//*[@id='fileinput']")
browse.click()
time.sleep(1)

upload_dialog = driver.switch_to_alert()
print (upload_dialog)

upload_dialog.send_keys("C:\\Users\\carlo.agresta\\Desktop\\V4LabCredentials.csv")

现在我的问题是我不能接受并关闭窗口 :S 有什么建议吗?

非常感谢提前

最佳答案

使用自动
示例代码:

import autoit
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

ActionChains(driver).move_to_element( driver.find_element_by_xpath("//div[@class='upload_button']")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle, "Edit1", "\\file\\path")
autoit.control_click(handle, "Button1")

关于Python Selenium 通过 Windows 上传上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46450677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com