但是,它不起作用.当我尝试alert.text('some text')或alert.send_keys(Keys.TAB)时,我不断收到错误NoAlertPresentException: Message: No alert is present,并且添加了要等待的命令后,我得到了错误TimeoutException: Message: Selenium是否无法识别我收到的弹出窗口(附带的屏幕截图)?如果是这样,我如何与他们互动?似乎很多人都必须使用它来保存和/或上传文件,但是我在Google上找不到任何东西.具体来说,我想为第一张图片选择保存文件",然后选择确定",对于第二张图片,我想浏览到该文件(即在文件名"字段中输入路径),然后单击打开".我不想只是将Firefox设置更改为自动保存,因为它最终将在不同的环境中运行,而这将无助于解决我的第二个问题.谢谢!我正在Windows上测试脚本,但最终将在Linux云服务器上实现.我以为我将不得不切换到PhantomJS webdriver(这可能会使我的问题更糟)来进行无头浏览,但是我找到了一种保留firefox的方法.我想这一切都意味着我无法使用AutoIT来解决问题. 解决方案您看到的弹出窗口不是可以使用 switch_to .这些弹出窗口是系统对话框,并且无法使用selenium 自动化.通常,人们通过调整浏览器首选项来避免首先显示这些对话框,例如: 使用硒下载文件 访问Firefox中的文件下载对话框 如何使用Selenium的WebDriver下载文件?对于上传,通常您可以找到适当的输入元素,并使用文件的路径向其发送键: 如何使用硒,python上传文件(图片) 如何将文件上传到文件输入中? (python-selenium文档) 让我知道您是否无法通过使用我随附的链接中的答案解决您的案件.对于第一个自动下载文件"问题,您只需设置正确的内容类型:profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/xml,text/xml")第二个问题修复程序(上载部分):driver.find_element_by_name("input_product_list").send_keys(textpath)driver.find_element_by_name('include_sr').click()driver.find_element_by_id('submit').click()I'm trying to use the Selenium module in python to generate a text list from one website, save it in a directory, and browse to that text list on another site to submit it.I'm working on the script in two parts- 1. Get metadata and 2. Order data. I've successfully completed the script in part 1, except for the very last thing: Choosing to save the metadata file that was just generated. I left it alone to work on part 2, hoping I would stumble upon the answer, but I'm just reaching to same problem when the pop-up to choose file comes along.In the documentation, I'm told that Selenium WebDriver has built-in support for handling popup dialog boxes and that after triggering a dialog box, if I call alert = driver.switch_to_alert() then I can "accept, dismiss, read its contents, or even type into a prompt."However, it's not working. When I try alert.text('some text') or alert.send_keys(Keys.TAB), I keep getting the error NoAlertPresentException: Message: No alert is present and after adding the command to wait, I get the error TimeoutException: Message:Are the popups I'm getting (screenshots attached) not recognized by Selenium? If so, how do I interact with them? It seems like using this to save and/or upload files is something that many people have to do, but I cannot find anything on Google. Specifically, I would like to choose 'Save File' then 'OK' for the first image and for the second I would like to browse to the file (i.e. enter the path into the file name field) and click 'Open.' I don't want to just change my Firefox settings to automatically save because this will eventually be run in a different environment, and that won't help solve my second problem.Thanks!EDIT:I'm testing my script on windows but it will eventually be implemented on a linux cloud server. I thought I was going to have to switch to PhantomJS webdriver (which was probably going to make my problem worse) to do headless browsing but I found a way to keep firefox. I guess all this means is that I can't use AutoIT to fix my problem. 解决方案 The popups you see are not regular popups that can be interacted with using switch_to. These popups are system dialogs and cannot be automated using selenium.Usually people avoid having these dialogs shown in the first place by tweaking browser preferences, e.g.:downloading file using seleniumAccess to file download dialog in FirefoxHow to download a file using Selenium's WebDriver?For uploading, usually you can find the appropriate input element and send keys to it with a path to the file:How to upload file ( picture ) with selenium, pythonHow to upload files into file inputs? (python-selenium docs)Let me know if your case cannot be solved by using the answers in the links I've attached.As for your first, "download file automatically" problem, you just need to set a correct content-type:profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/xml,text/xml")Second problem fix (upload part):driver.find_element_by_name("input_product_list").send_keys(textpath)driver.find_element_by_name('include_sr').click()driver.find_element_by_id('submit').click() 这篇关于在python中使用硒与弹出框进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-01 23:19