【问题标题】:How to click on a button (angular) via python script selenium如何通过 python 脚本 selenium 单击按钮(角度)
【发布时间】:2014-07-18 08:16:32
【问题描述】:

我是 selenium、python 和 angular 的新手。我设法用 Python 和 selenium 运行了一些测试;但是,当涉及到 angularJS 时,我发现很难单击按钮。按钮的代码是:

<button type="button"
        class="xyz"
        ng-click="validateList()"
        id="home-xyz-button">
  <i class="xyz-up"></i>Validate
</button>

我运行代码:

browser.find_element_by_id("home-xyz-button").click()

这不起作用。

【问题讨论】:

  • 怎么不行?如果有例外,您可以编辑您的问题并添加它吗?发布更大的 HTML 部分也很有帮助,这样我们就可以帮助您找到选择器。有太多我能想到的可能是错误的:ID 出现多次。 ID 是动态的。元素包含在框架中。
  • 不起作用是什么意思?

标签: python angularjs selenium


【解决方案1】:

下面是我的脚本:

import os, sys
from selenium import webdriver

# Load the firefox driver
browser = webdriver.Firefox()

# Open a browser and browser to website 
browser.get('link to the website')

browser.implicitly_wait(10)

# Select File for Validation
SelectFileForValidation = browser
  .find_element_by_xpath("//div[@class=\"row ng-scope\"]"+
                         "/div/div/table/tbody/tr[1]/td[1]/input")
if SelectFileForValidation is None:
    print('SelectFileForValidation is not found')
    exit(0)
else:
    print('SelectFileForValidation is found')
    SelectFileForValidation.click()

# Get the file name
FindTheUploadedFile = browser.find_element_by_xpath("//div[@class=\"row ng-scope\"]/div/div/table/tbody/tr[1]/td[3]/div[@class=\"ng-binding\"]")
if FindTheUploadedFile is None:
    print('FindTheUploadedFile is not found')
    exit(0)
else:
    print('FindTheUploadedFile is found')
    print FindTheUploadedFile.text 

ValidateFileButton = browser.find_element_by_xpath("//button[@id=\"home-xyz-button\"]")
if ValidateFileButton is None:
    print('ValidateFileButton is not found')
    exit(0)
else:
    print('ValidateFileButton is found')
    ValidateFileButton.ng-click()


#browser.get('link to the site (used to refresh)')

exit(0)

HTML代码是:

<div class="111">
    <button type="button" class="222" data-toggle="modal" data-target="#uploadFileModal" id="kkk">
        <i class="333"></i> Upload
    </button>
    <button type="button" class="xyz" ng-click="validateList()" id="home-xyz-button">
        <i class="xyz-up"></i> Validate
    </button>
    <button disabled="true" type="button" class="444" id="aaaa">
        <i class="555"></i> Download
    </button>
    <button disabled="true" type="button" class="666" id="bbbb">
        <i class="777"></i> Delete
    </button>
    <!-- Modal -->
    <div class="888" id="cccc" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="999">
            <div class="101010">
                <div class="11111">
                    <button type="button" class="121212" data-dismiss="modal" aria-hidden="true">×</button>
                    <h2 class="131313" id="dddd">Upload File</h2>
                </div>
                <div class="141414">
                    Browse to the file you intend to upload. <br> <br>
                    <input type="file" name="file" id="eee" onchange="angular.element(this).scope().storeFile(this)"> <br>
                    <input type="checkbox" id="fff> Overwrite existing file
                </div>
                <div class="151515">
                    <button type="button" class="161616" data-dismiss="modal" id="ggg">Close</button>
                    <button type="submit" class="171717" id="hhh" ng-click="uploadFile()">Upload</button>
                </div>
            </div>
        </div>
    </div>
    <!-- End Modal -->
</div>

我得到的打印输出是:

SelectFileForValidation is found
FindTheUploadedFile is found
"The file Name"
ValidateFileButton is found

但我看不到正在执行 click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 2016-07-15
    相关资源
    最近更新 更多