[英]Selenium. Upload a file on a new chrome open file pop up (or how to simulate ctrl+v on python)
Im doing a python selenium script for chrome webdriver, and I need to upload a file. 我正在为chrome webdriver做一个python selenium脚本,我需要上传一个文件。 When I hit the upload buttom, a open file dialog pops up, and I haven't been able to actually upload the file.
当我点击上传按钮时,弹出一个打开的文件对话框,我无法实际上传文件。 Aproaches I have tried:
我试过的Aproaches:
Using selenium ide, and recording me uploading the file. 使用selenium ide,并记录我上传文件。 It yeilds this result:
它产生了这个结果:
driver.find_element_by_id("html5_1aq7art6u98a1i1eokfk581u443").clear()
driver.find_element_by_id("html5_1aq7art6u98a1i1eokfk581u443").send_keys("")
However, the id changes everytime the page is reloaded, and so this aproach doesnt work. 但是,每次重新加载页面时,id都会更改,因此这种方法无法正常工作。 I also haven't been able to find a way to locate the form to type the file name.
我也找不到找到表单来键入文件名的方法。
I also tried using alerts and changing window with selenium 我也尝试使用警报并用硒改变窗口
driver.switch_to_window("Open")
driver.send_keyssend_keys(os.getcwd()+"/file.png")
alert = driver.switch_to.alert
alert.send_keyssend_keys(os.getcwd()+"/file.png")
But both of this aproaches have failed. 但这两种方法都失败了。 (the first one doesnt finds the window, and the second one tells me that no alert is open)
(第一个没有找到窗口,第二个告诉我没有打开警报)
As a last resort Im searching how to copy to clipboard the name of the file, and after clicking the upload buttom, wating something like a second, and then simulating with python a "crtl+v" followed by "enter" key press, but I dont know how to do it, and the internet searchs take me to the dark path of some c based third party libraries that overwhelmed me a bit. 作为最后的手段我在搜索如何复制到剪贴板的文件的名称,并在点击上传按钮后,等待类似于秒,然后使用python模拟“crtl + v”,然后按“enter”键按,但我不知道该怎么做,互联网搜索把我带到一些基于c的第三方库的黑暗路径,这让我有些不知所措。
Here is the full code for the posting script, the rest of the code (before this part) its just scrapping and deleting my own post: 以下是发布脚本的完整代码,其余代码(在此部分之前)仅删除并删除我自己的帖子:
driver.get('https://www.kijiji.ca/p-post-ad.html?categoryId=87')
time.sleep(2)
driver.find_element_by_css_selector("button.button--jss-0-514").click()
driver.find_element_by_id("postad-title").clear()
driver.find_element_by_id("postad-title").send_keys(postTitle)
driver.find_element_by_id("pstad-descrptn").clear()
driver.find_element_by_id("pstad-descrptn").send_keys(postContent)
driver.find_element_by_id("pstad-map-address").clear()
driver.find_element_by_id("pstad-map-address").send_keys(postAdress)
#driver.find_element_by_id("ImageUploadButton").click()
#time.sleep(2)
driver.find_element_by_xpath("//input[@type='file'][contains(@id,'html5_')]").clear()
driver.find_element_by_xpath("//input[@type='file'][contains(@id,'html5_')]").send_keys("file.png")
driver.find_element_by_id("PhoneNumber").clear()
driver.find_element_by_id("PhoneNumber").send_keys(phoneNumber)
driver.find_element_by_id("pstad-email").send_keys(profile)
driver.find_element_by_name("saveAndCheckout").click()
Full Error: [10360:7760:0815/140356:ERROR:latency_info.cc(164)] RenderWidgetHostImpl::OnSwapCompositorFrame, LatencyInfo vector size 187 is too big. 完整错误:[10360:7760:0815/140356:错误:latency_info.cc(164)] RenderWidgetHostImpl :: OnSwapCompositorFrame,LatencyInfo矢量大小187太大。 [10360:7760:0815/140357:ERROR:latency_info.cc(164)] RenderWidgetHostImpl::OnSwapCompositorFrame, LatencyInfo vector size 452 is too big.
[10360:7760:0815/140357:ERROR:latency_info.cc(164)] RenderWidgetHostImpl :: OnSwapCompositorFrame,LatencyInfo矢量大小452太大。 Traceback (most recent call last): File "KijijiAutoPosting.py", line 95, in driver.find_element_by_xpath("//input[@type='file'][contains(@id,'html5_')]").clear() File "C:\\Users\\Silve\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\remote\\webelement.py", line 87, in clear self._execute(Command.CLEAR_ELEMENT) File "C:\\Users\\Silve\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\remote\\webelement.py", line 461, in _execute return self._parent.execute(command, params) File "C:\\Users\\Silve\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 236, in execute self.error_handler.check_response(response) File "C:\\Users\\Silve\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py", line 192, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidElementStateException: Message: invalid eleme
回溯(最近一次调用最后一次):文件“KijijiAutoPosting.py”,第95行,在driver.find_element_by_xpath(“// input [@ type ='file'] [contains(@ id,'html5 _')]”)。清除()文件“C:\\ Users \\ Silve \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib \\ site-packages \\ selenium \\ webdriver \\ remote \\ webelement.py”,第87行,清除self._execute(命令.CLEAR_ELEMENT)文件“C:\\ Users \\ Silve \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib \\ site-packages \\ selenium \\ webdriver \\ remote \\ webelement.py”,第461行,在_execute中返回self._parent .execute(command,params)文件“C:\\ Users \\ Silve \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib \\ site-packages \\ selenium \\ webdriver \\ remote \\ webdriver.py”,第236行,执行中self.error_handler.check_response(response)文件“C:\\ Users \\ Silve \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib \\ site-packages \\ selenium \\ webdriver \\ remote \\ errorhandler.py”,第192行, check_response raise exception_class(message,screen,stacktrace)selenium.common.exceptions.InvalidElementStateException:消息:invalid eleme nt state: Element is not currently interactable and may not be manipulated (Session info: chrome=52.0.2743.116) (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64)
nt state:元素当前不可交互,可能无法操作(会话信息:chrome = 52.0.2743.116)(驱动程序信息:chromedriver = 2.9.248315,platform = Windows NT 6.3 x86_64)
Try following code: 请尝试以下代码:
driver.find_element_by_xpath("//input[@type='file']").clear()
driver.find_element_by_xpath("//input[@type='file']").send_keys("/path/to/file")
This should work if there is only one button for file uploading. 如果只有一个文件上传按钮,这应该有效。
Also you can try this XPath
: 你也可以尝试这个
XPath
:
"//input[@type='file'][contains(@id,'html5_')]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.