0

Could you help to fix this error. Received error as:

Unable to find element with xpath

Below the tags for the element which is a Text area

<td nowrap="">
<script> 
function clipBrdAction(evt, fieldName) { 
    evt = evt||window.event;     
    if(evt.ctrlKey && evt.keyCode==67) {      
        document.execCommand('copy');      
        //var temp = $(fieldName).value;      
        //clipboardData.setData('Text', temp);      
        //$(fieldName).value = clipboardData.getData('Text');     
    }     
    else if(evt.ctrlKey && evt.keyCode==86) {
        document.execCommand('paste');      
        //$(fieldName).value = clipboardData.getData('Text');     
    }     
    else if(evt.ctrlKey && evt.keyCode==88) {      
        document.execCommand('cut');      
        //var temp = $(fieldName).value;      
        //clipboardData.setData('Text', temp);      
        //$(fieldName).value = '';     
    } else if(evt.keyCode==46) {      
        document.execCommand('delete');      
        //var selStart = $(fieldName).selectionStart;      
        //var selEnd = $(fieldName).selectionEnd;      
        //var fieldLen = $(fieldName).value.length;      
        //if (selEnd == fieldLen)         
        // temp = $(fieldName).value.substr(0,selStart);      
    //else if (selEnd > selStart)      
        // temp = $(fieldName).value.substr(0,selStart)+ $(fieldName).value.substr(selEnd, fieldLen);     
    //else      
        // temp = $(fieldName).value.substr(0,selStart)+ $(fieldName).value.substr((+selEnd + +1), fieldLen);      

    //clipboardData.setData('Text', temp);      
    //$(fieldName).value = clipboardData.getData('Text');     
} 
</script>

<textarea id="nrpsFilter" rows="3" onkeypress="return checkIt(event,'');" cols="30" title="" onblur="this.value=trim(this.value);" onkeyup="" onkeydown="return clipBrdAction(event, 'nrpsFilter');" name="nrpsFilter"/>
</td>

And my code is:

driver.findElement(By.xpath("//*[@id='nrpsFilter']")).sendKeys("*1??0*");

In Firepath, the element is identifiable, after when i search for another element that is under same division (div tag) but not identifiable after searching for an element from different division. Does that mean I should Switch to the division in my code? Both the divisions are under same Frame.

cakan
  • 2,099
  • 5
  • 32
  • 42
Madhav
  • 21
  • 3

2 Answers2

0

Ok then just before using Sendkeys please switch to the iframe first after that your code will work without any problem.

driver.switchTo().frame("frame name");
// perform your action 
// after you have performed all your action inside the iframe 
// plz switch back to the default content 

driver.switchTo().defaultContent(); 
// now perform your user action outside that iframe

Hope this helps you

Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
  • also plz have a look at http://stackoverflow.com/questions/9942928/how-to-handle-iframe-in-webdriver – Rajnish Kumar Jun 17 '16 at 10:30
  • Thanks Raj. I added the switchTo() function and now the code looks like this: – Madhav Jun 17 '16 at 11:49
  • 'driver.switchTo().frame("bodyframe"); driver.findElement(By.xpath("//*[@id='first']//span")).click(); //(1. Click Dropdown') driver.findElement(By.xpath("//*[@id='ui-id-33']/span")).click(); //(2. Click SubDropDown ) driver.findElement(By.xpath("//a[contains(text(),'Quick Search')][@id='referential_quicksearch']")).click(); //(3.Click the link) driver.switchTo().defaultContent(); //driver.switchTo().frame("bodyframe"); driver.findElement(By.xpath("//*[@id='nrpsFilter']")).sendKeys("1?0*");//(Enter in textArea)' – Madhav Jun 17 '16 at 11:59
  • did it worked ? Also why are you switching to same frame two times ? Switching just before sendkeys will also work – Rajnish Kumar Jun 17 '16 at 12:10
  • No Raj it didnt work. Yes, i need to change from BANNER to the frame(bodyframe), so need to switch initially(Doesn't work without this statement). Could you advise if Im missing something? – Madhav Jun 20 '16 at 10:48
0

Try this and see if it works. I took it from your comment but rearranged a couple statements. I think you want the final sendkeys() to be in the IFRAME also, right? We really need more complete HTML to help any further.

driver.findElement(By.xpath("//*[@id='first']//span")).click(); // (1. Click Dropdown)
driver.findElement(By.xpath("//*[@id='ui-id-33']/span")).click(); // (2. Click SubDropDown )
driver.findElement(By.xpath("//a[contains(text(),'Quick Search')][@id='referential_quicksearch']")).click(); // (3.Click the link)
driver.findElement(By.xpath("//*[@id='nrpsFilter']")).sendKeys("1?0*");// (Enter in textArea)
driver.switchTo().defaultContent();
JeffC
  • 22,180
  • 5
  • 32
  • 55