在python中使用硒与弹出框进行交互 [英] Interacting with pop-up boxes using selenium in python

查看:109
本文介绍了在python中使用硒与弹出框进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python中的Selenium模块从一个网站生成文本列表,将其保存在目录中,然后浏览至另一站点上的该文本列表以进行提交.

我正在将脚本分为两个部分:1.获取元数据和2.订购数据.除了最后一件事,我已经成功完成了第1部分中的脚本:选择保存刚刚生成的元数据文件.我把它留给第二部分工作,希望我会偶然发现答案,但是当弹出选择文件的弹出框时,我只是遇到了同样的问题.

文档中,我是告诉Selenium WebDriver内置了处理弹出对话框的支持,并且在触发对话框后,如果我调用alert = driver.switch_to_alert(),那么我可以接受,关闭,读取其内容,甚至键入提示."

但是,它不起作用.当我尝试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来解决问题.

解决方案

您看到的弹出窗口不是可以使用使用硒下载文件

  • 访问Firefox中的文件下载对话框
  • 如何使用Selenium的WebDriver下载文件?
  • 对于上传,通常您可以找到适当的输入元素,并使用文件的路径向其发送键:

    让我知道您是否无法通过使用我随附的链接中的答案解决您的案件.


    对于第一个自动下载文件"问题,您只需设置正确的内容类型:

    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.:

    For uploading, usually you can find the appropriate input element and send keys to it with a path to the file:

    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中使用硒与弹出框进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆