<small id='3T9BG'></small><noframes id='3T9BG'>

  1. <i id='3T9BG'><tr id='3T9BG'><dt id='3T9BG'><q id='3T9BG'><span id='3T9BG'><b id='3T9BG'><form id='3T9BG'><ins id='3T9BG'></ins><ul id='3T9BG'></ul><sub id='3T9BG'></sub></form><legend id='3T9BG'></legend><bdo id='3T9BG'><pre id='3T9BG'><center id='3T9BG'></center></pre></bdo></b><th id='3T9BG'></th></span></q></dt></tr></i><div id='3T9BG'><tfoot id='3T9BG'></tfoot><dl id='3T9BG'><fieldset id='3T9BG'></fieldset></dl></div>

  2. <tfoot id='3T9BG'></tfoot>
      <bdo id='3T9BG'></bdo><ul id='3T9BG'></ul>

    <legend id='3T9BG'><style id='3T9BG'><dir id='3T9BG'><q id='3T9BG'></q></dir></style></legend>

      通过 webdriver 点击 javascript 弹出窗口

      Click the javascript popup through webdriver(通过 webdriver 点击 javascript 弹出窗口)

          <bdo id='7c2nQ'></bdo><ul id='7c2nQ'></ul>
          <i id='7c2nQ'><tr id='7c2nQ'><dt id='7c2nQ'><q id='7c2nQ'><span id='7c2nQ'><b id='7c2nQ'><form id='7c2nQ'><ins id='7c2nQ'></ins><ul id='7c2nQ'></ul><sub id='7c2nQ'></sub></form><legend id='7c2nQ'></legend><bdo id='7c2nQ'><pre id='7c2nQ'><center id='7c2nQ'></center></pre></bdo></b><th id='7c2nQ'></th></span></q></dt></tr></i><div id='7c2nQ'><tfoot id='7c2nQ'></tfoot><dl id='7c2nQ'><fieldset id='7c2nQ'></fieldset></dl></div>

          <small id='7c2nQ'></small><noframes id='7c2nQ'>

        • <tfoot id='7c2nQ'></tfoot>
          1. <legend id='7c2nQ'><style id='7c2nQ'><dir id='7c2nQ'><q id='7c2nQ'></q></dir></style></legend>

                  <tbody id='7c2nQ'></tbody>
                本文介绍了通过 webdriver 点击 javascript 弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 Python 中使用 Selenium webdriver 抓取网页

                I am scraping a webpage using Selenium webdriver in Python

                我正在处理的网页有一个表格.我可以填写表格,然后点击提交按钮.

                The webpage I am working on, has a form. I am able to fill the form and then I click on the Submit button.

                它会生成一个弹出窗口(Javascript Alert).我不确定,如何通过 webdriver 点击弹出窗口.

                It generates an popup window( Javascript Alert). I am not sure, how to click the popup through webdriver.

                知道怎么做吗?

                谢谢

                推荐答案

                Python Webdriver 脚本:

                Python Webdriver Script:

                from selenium import webdriver
                
                browser = webdriver.Firefox()
                browser.get("http://sandbox.dev/alert.html")
                alert = browser.switch_to_alert()
                alert.accept()
                browser.close()
                

                网页(alert.html):

                Webpage (alert.html):

                <html><body>
                    <script>alert("hey");</script>
                </body></html>
                

                运行 webdriver 脚本将打开显示警报的 HTML 页面.Webdriver 立即切换到警报并接受它.Webdriver 然后关闭浏览器并结束.

                Running the webdriver script will open the HTML page that shows an alert. Webdriver immediately switches to the alert and accepts it. Webdriver then closes the browser and ends.

                如果您不确定是否会出现警报,那么您需要使用类似的方法来捕获错误.

                If you are not sure there will be an alert then you need to catch the error with something like this.

                from selenium import webdriver
                
                browser = webdriver.Firefox()
                browser.get("http://sandbox.dev/no-alert.html")
                
                try:
                    alert = browser.switch_to_alert()
                    alert.accept()
                except:
                    print "no alert to accept"
                browser.close()
                

                如果需要查看alert的文本,可以通过访问alert对象的text属性来获取alert的文本:

                If you need to check the text of the alert, you can get the text of the alert by accessing the text attribute of the alert object:

                from selenium import webdriver
                
                browser = webdriver.Firefox()
                browser.get("http://sandbox.dev/alert.html")
                
                try:
                    alert = browser.switch_to_alert()
                    print alert.text
                    alert.accept()
                except:
                    print "no alert to accept"
                browser.close()
                

                这篇关于通过 webdriver 点击 javascript 弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                env: python: No such file or directory(env: python: 没有这样的文件或目录)
                How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

                    <tbody id='6mylH'></tbody>

                    <i id='6mylH'><tr id='6mylH'><dt id='6mylH'><q id='6mylH'><span id='6mylH'><b id='6mylH'><form id='6mylH'><ins id='6mylH'></ins><ul id='6mylH'></ul><sub id='6mylH'></sub></form><legend id='6mylH'></legend><bdo id='6mylH'><pre id='6mylH'><center id='6mylH'></center></pre></bdo></b><th id='6mylH'></th></span></q></dt></tr></i><div id='6mylH'><tfoot id='6mylH'></tfoot><dl id='6mylH'><fieldset id='6mylH'></fieldset></dl></div>
                        • <bdo id='6mylH'></bdo><ul id='6mylH'></ul>
                        • <tfoot id='6mylH'></tfoot>

                          <small id='6mylH'></small><noframes id='6mylH'>

                          <legend id='6mylH'><style id='6mylH'><dir id='6mylH'><q id='6mylH'></q></dir></style></legend>