我正在尝试 Mechanize Apple Dev Portal“Edit iOS Provisioning Profile”的选择设备部分,可以找到 here (如果您已登录)。
源看起来像这样:
<form name="profileEdit" method="get" action="https://developer.apple.com/services-developerportal/QH43B2/account/ios/profile/regenProvisioningProfile.action?content-type=text/x-url-arguments&accept=application/json&requestId=838c910b-f63d-843e7b1ce126&userLocale=en_US&teamId=BF5K33D" successURL="/account/ios/profile/profileDownload.action?provisioningProfileId=">
<input type="hidden" name="distributionType" value='store'/>
<input type="hidden" name="returnFullObjects" value="false"/>
<div class="nameSection">
<dl>
<dt class="selectDevices">Devices:</dt>
<dd class="selectDevices">
<div class="table">
<div class="rows">
<div><input type="checkbox" name="deviceIds" class="validate" value="8T8RG7HX" id="devices-6" ><span class="title">iPhone 4 - JC</span></div>
<div><input type="checkbox" name="deviceIds" class="validate" value="7Y9F8N47" id="devices-7" ><span class="title">iPhone 5 - DP</span></div>
<div><input type="checkbox" name="deviceIds" class="validate" value="ZNES97W7" id="devices-8" checked><span class="title">iPhone 5 - JC</span></div>
<div><input type="checkbox" name="deviceIds" class="validate" value="CRDSL7S5" id="devices-9" checked><span class="title">iPod 4 inch</span></div>
</div>
</div>
</dd>
<dd class="form-error deviceIds hidden">Please select a Device</dd>
</dl>
</div>
<div class="bottom-buttons">
<a class="button small left cancel"><span>Cancel</span></a>
<a class="button small blue right submit"><span>Generate</span></a>
</div>
</form>
我想要做的是选中所有框:
form = page.form_with(:name => 'profileEdit') or raise UnexpectedContentError
form.checkboxes_with(:name => 'deviceIds').each do |checkbox|
puts checkbox["id"] # prints correct value of devices-6...
checkbox.check
end
form.method = 'GET'
form.submit
我没有遇到运行时错误,但是当我刷新实际页面时,并非所有复选框都按照我的意图被选中。我错过了什么吗?
最佳答案
为了这:
form.checkboxes_with(:name => 'deviceIds').each do |checkbox|
puts checkbox["id"] # prints correct value of devices-6...
checkbox.check
end
这些会导致什么:
tmp1 = form.checkboxes_with(:name => 'deviceIds').map { |cb| cb.check }
tmp2 = form.checkboxes_with(:name => 'deviceIds').map { |cb| cb.checked? }
我希望
[true, true, true, true]
对彼此而言。如果没有,有些东西正在清除它们。 check()
方法在 RadioButton
中实现这确实清除了所有同名按钮的选中状态,但应该仅限于 radiobutton
类型。 checked
属性本身是可写的,因此您可以尝试直接编写它:form.checkboxes_with(:name => 'deviceIds').each do |cb|
cb.checked = true
end
并避免该页面/Mechanize/任何内容中可能存在的错误或不一致。只是一个猜测,但有些东西可以尝试。
关于ruby - 带有 checkbox.check 的 Mechanize 复选框不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18852544/