如何通过Python脚本Selenium点击Angular按钮
我刚接触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()
但是这并没有成功。
1 个回答
-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()这个函数被执行