[英]Locating button with xpath - JAVA / Selenium 2 in Chrome
按鈕后面的代碼:
<input class=”btn” id=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ name=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ onclick=”window.open(‘/apex/newscenario?Opportunity__c=006f00000072n8hAAA’,’_top’, 1);;A4J.AJAX.Submit(‘mypage:formid’,event,{‘similarityGroupingId’:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′,’parameters’:{‘mypage:formid:relatedScenaiosListId:j_id27:j_id28′:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′} } );return false;” value=”New” type=”button”>
我從“檢查元素”視圖中右鍵單擊,發現可以復制Xpath並發現它是:
//*[@id="mypage:formid:relatedScenaiosListId:j_id27:j_id28"]
注意*。
我累了:
WebElement txtnew = driver.findElement(By.xpath(“//input[@id='mypage:formid:relatedScenaiosListId:j_id27:j_id28']“));
txtnew.click();
和
WebElement txtnew = driver.findElement(By.xpath(“//input[@id='mypage:formid:relatedScenaiosListId:j_id27:j_id28'][@value='New']“));
txtnew.click();
但都沒有奏效。
我對*感到好奇,如果那應該成為我的Xpath語句的一部分?
如果您不需要使用xpath,請使用按ID搜索或使用cssSelectors查找元素。 例如
//if you have only one element with class btn you can use this selector
//if element placed in parent (and this can identify element much more) add selector to
//parent before .btn
WebElement txtnew = driver.findElement(By.Css(".btn"));
//or
WebElement txtnew = driver.findElement(By.Css("input[value='New']"));
//or if id not generated automatically
WebElement txtnew = driver.findElement(By.Css("#mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
//or using By.Id
WebElement txtnew = driver.findElement(By.Id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
他們中的任何一個都將在特定情況下工作。 選擇一種更適合您的情況。 謝謝。
試試這個xpath:
input[@value='New'][@class='btn'][starts-with(@id, 'mypage:formid')]
我沒有意識到的真正問題是控件位於不同的框架中。 一旦我將驅動程序設置為查看框架,便可以使用任何數量的xpath表達式。
`driver.manage()。timeouts()。implicitlyWait(25,TimeUnit.SECONDS); driver.switchTo()。frame(“ 066i0000004bNpx”);
WebElement txtNewbtn = driver.findElement(By.id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
txtNewbtn.click();`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.