2014-01-06 53 views
0
<div id="div_12_1_1_1_3_1_2_1_1_1_2" class="Quantity CoachView CoachView_show" data-eventid="" data-viewid="qty" data-config="config12" data-bindingtype="Decimal" data-binding="local.priceBreak.quantity" data-type="com.ibm.bpm.coach.Snapshot_a30ea40f_cb24_4729_a02e_25dc8e12dcab.Quantity"> 
<div class="w-decimal w-group clearfix"> 
<div class="p-label-container span4"> 
<div class="p-fields-container controls-row span8 l-input fixed-units"> 
<input id="div_12_1_1_1_3_1_2_1_1_1_2-in" class="p-field span8" type="text" maxlength="16"> 
<input id="div_12_1_1_1_3_1_2_1_1_1_2-iu" class="p-unit span4" type="text" maxlength="2" style="display: none;"> 
<select class="p-unit span4" style="display: none;"></select> 
<div class="p-unit span4">CM</div> 
<div class="p-help-block"></div> 
</div> 
<div class="p-fields-container span8 l-output" style="display: none;"> 
</div> 
</div> 
<div id="div_12_1_1_1_3_1_2_1_1_1_3" class="Quantity CoachView CoachView_show"   data-eventid="" data-viewid="Quantity2" data-config="config73" data-bindingtype="Integer" data-binding="local.priceBreak.numberDeliveries" data-type="com.ibm.bpm.coach.Snapshot_a30ea40f_cb24_4729_a02e_25dc8e12dcab.Quantity"> 

這裏怎麼點擊其ID的文本框「div_12_1_1_1_3_1_2_1_1_1_2式」 但對於一些情況的變化,以「div_5_1_1_1_3_1_2_1_1_1_2-在「如何點擊文本字段「數量」的ID值在不同的情況下改變

我曾嘗試用下面,

driver.findElement(By.xpath("//div/input[ends-with(@id,'__1_1_1_3_1_2_1_1_1_2-in')]")).sendKeys("98989998989"); 

,但它不工作..

輸出:

org.openqa.selenium.InvalidSelectorException:給定的選擇器// div/input [ends-with(@id,'__ 1_1_1_3_1_2_1_1_1_2-in')]無效或不導致WebElement。發生以下錯誤: InvalidSelectorError:由於以下錯誤,無法找到xpath表達式// div/input [ends-with(@id,'__ 1_1_1_3_1_2_1_1_1_2-in')]中的元素: [Exception ...「這個表達不是一個合法的表達。「代碼:「51」nsresult:「0x805b0033(NS_ERROR_DOM_INVALID_EXPRESSION_ERR)」location:「file:/// C:/Users/SUNIL~1.WAL/AppData/Local/Temp/anonymous4157273428687139624webdriver-profile/extensions/[email protected]/組件/ driver_component.js行:5956「] 命令持續時間或超時:41毫秒 有關此錯誤的文檔,請訪問:http://seleniumhq.org/exceptions/invalid_selector_exception.html 構建信息:版本:'2.37.0',修訂:'a7c61cb',時間:'二○一三年十月一十八日17時15分02' 秒

+0

您在xpath表達式中測試的字符串的開頭有兩個下劃線,而HTML僅顯示單數下劃線。 – t0mppa

回答

0

你可以用下面的cssSelector嘗試,

driver.findElement(By.cssSelector("div.fixed-units > input[id$='in']")).sendKeys("98989998989"); 
0

如果‘中’文本存在總是那麼你可以使用XPath。你可以試試// DIV [包含( '在' @id)]

0

ends-with的XPath 2查詢,其中五大actually support v2

你選擇沒有要麼使用其他方法已經建議,或一個XPath 1解決方案,您可以使用:

//div/input[substring(@id, string-length(@id) - 22) = '_1_1_1_3_1_2_1_1_1_2-in'] 

雖然這是醜陋的,真的。

0

我實際上使用了「starts-with」......但是我看到你有多個以「div」開頭的東西。

如果您的元素都保留在頁面上的相同位置並且不會更改,請嘗試此操作。下面是一些Java代碼:

By by = By.xpath("(//*[starts-with(@" + attributeName + ", '" + attributeValue + "')])[" + n + "]"); 

在你的情況下,它應該是這樣的:

By by = By.xpath("(//*[starts-with(@id, 'div')])[2]"); 

這將完成的就是選擇與DOM中的「格」開始的第二個元素。

這是一個黑客...但它可能會爲你工作。

相關問題