简体   繁体   中英

Locating button with xpath - JAVA / Selenium 2 in Chrome

Code behind button:

<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”>

I did a right click from the Inspect Element view and saw I could copy the Xpath and found it was :

//*[@id="mypage:formid:relatedScenaiosListId:j_id27:j_id28"]

Note the *.

I tired:

WebElement txtnew = driver.findElement(By.xpath(“//input[@id='mypage:formid:relatedScenaiosListId:j_id27:j_id28']“));
txtnew.click();

and

WebElement txtnew = driver.findElement(By.xpath(“//input[@id='mypage:formid:relatedScenaiosListId:j_id27:j_id28'][@value='New']“));
txtnew.click();

but neither worked.

I'm curious about the *, if that should be part of my Xpath statement?

If it's not necessary for you to use xpath, use searching by id, or cssSelectors to find your element. Eg

//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"));

Any of them will work in specific situation. Choose one which more suitable for your sitution. Thanks.

试试这个xpath:

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

The real issue, that I didn't realize, was that the control was in a different frame. Once I set the driver to look at the frame any number of xpath expressions worked.

` 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();`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM