2014-08-28 30 views
0

代碼:使用XPath定位按鈕 - JAVA /硒2在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語句的一部分?

+0

你是什麼意思的「不工作」 - 任何錯誤? – alecxe 2014-08-28 22:42:48

回答

0

真正的問題,我不知道,是控制在不同的幀。一旦我設置驅動程序來查看框架的任何數量的XPath表達式的工作。 (25,TimeUnit.SECONDS);其中,驅動程序可以使用一個或多個驅動程序。 driver.switchTo()。frame(「066i0000004bNpx」);

WebElement txtNewbtn = driver.findElement(By.id("mypage:formid:relatedScenaiosListId:j_id27:j_id28")); 
txtNewbtn.click();` 
1

如果您不需要使用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")); 

其中任何一個都可以在特定情況下工作。選擇一個更適合你的打折。謝謝。

1

試試這個XPath:

input[@value='New'][@class='btn'][starts-with(@id, 'mypage:formid')]