[英]How to find a field using placeholder text in Watir?
我有兩個搜索字段,我想找到第二個來設置一些文本。
我嘗試使用div,但是它總是會找到第一個搜索字段。 是否有人對如何找到第二個對象或使用唯一的占位符文本“ Search ...”提出建議?
HTML:
input type="text" name="searchString" id="searchString" projects="" for="" placeholder="Search" class="form-control"
input type="text" name="searchString" id="searchString" placeholder="Search..." class="form-control"
Ruby-Watir:
@b.link(:text => "http://ml-test.mytest.com/Client/Profile/ab295b41-2c5e-4100-bdee-e757405238bb").click
@b.text_field{div(:class => "col-sm-4 col-md-3", :placeholder => "Search...")}.set "Automation (Test)"
看來問題可能只是代碼中的錯字。 請注意以下行:
@b.text_field{div(:class => "col-sm-4 col-md-3", :placeholder => "Search...")}.set "Automation (Test)"
{}
所見的塊正被傳遞給text_field
方法。 text_field
方法對塊不執行任何操作,因此代碼實際上只是在做:
@b.text_field.set "Automation (Test)"
由於沒有向文本字段提供定位器,因此它將在頁面上輸入第一個文本字段。
要基於占位符屬性(或任何其他定位符)定位文本字段,需要將其作為參數而不是塊傳遞:
@b.text_field(:placeholder => "Search...").set "Automation (Test)"
您還包括了一個div。 假設它是一個祖先元素,需要查找文本字段,則應為:
@b.div(:class => "col-sm-4 col-md-3").text_field(:placeholder => "Search...").set "Automation (Test)"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.