0

I am using Selenium JAVA. The code below generates an error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"name","selector":"p_subj_code"} Command duration or timeout: 167 milliseconds.

Here is what my code should do:

  • Open an application
  • Search for the course
  • Click on the course
  • click on the copy course link
  • opens a new window
  • click on the Element(this is a drop down menu) and enter the Value in it.

I am getting the error when it opens a new window and then it is unable to locate the element.

My Java code:

driver.findElement(By.xpath("//a[contains(text(),'Course Outline Editor')]")).click();

//Search the course
driver.findElement(By.xpath("//input[@value='Submit']")).click(); 
driver.findElement(By.xpath("//select[@id='majr_code_id']")).sendKeys("BMSC");
//Enter values for  course

driver.findElement(By.xpath("//input[@name='p_crse_numb_low']")).sendKeys("234");

//Enter values for  course
driver.findElement(By.xpath("//input[@name='p_submit_button']")).click();

//Submits the search
driver.findElement(By.xpath("//a[contains(text(),'BMSC 234')]")).click();

//Brings up the course
String winHandleBefore = driver.getWindowHandle();
System.out.println("Window title" + driver.getTitle()); 
driver.findElement(By.xpath("html/body/div[3]/a[2]/font")).click();

//Click the link to open a new page
driver.findElement(By.name("p_subj_code")).click(); 

//(Opens a new page)
driver.findElement(By.xpath(".//*[@id='majr_code_id']/option[2]")).click();       
driver.findElement(By.xpath("//input[@id='p'_crse_numb']")).sendKeys("111");
driver.findElement(By.xpath("//input[@value='Find']")).click();

Corresponding HTML code:

<caption class="captiontext">Find Course to Copy from</caption>
<tbody>
<tr>
<td class="dedefault"/>
</tr>
<tr>
<td class="delabel" width="120" scope="row">Subject Code:</td>
<td class="dedefault" width="100" colspan="3">
<p class="leftaligntext">
<select id="majr_code_id" size="1" name="p_subj_code">
<option value="">Select  </option>
<option value="ACCY">ACCY </option>
Community
  • 1
  • 1
  • If you do those steps manually, on the Developer Console, are you able to get the right workflow? – Hackerman Aug 11 '16 at 21:23
  • 1
    first suggestion is always the same: try to wait for your element before clicking on it. i.e. instead of `driver.findElement(By.name("p_subj_code")).click(); ` do `WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.elementToBeClickable(By.name("p_subj_code"))).click();` – timbre timbre Aug 11 '16 at 21:49
  • check the response of driver.findElement(By.xpath("html/body/div[3]/a[2]/font")).click(); I doubt if it clicking on the link. – ChanChow Aug 11 '16 at 22:23
  • Element by name `p_subj_code` is in same window or it is in new window??? – Saurabh Gaur Aug 12 '16 at 01:05
  • @Kiril S Unfortunately your suggestion did not work.Thanks for your effort in looking at it . – shinymoon Aug 14 '16 at 21:42
  • @AlllsWell, It is clicking on driver.findElement(By.xpath("html/body/div[3]/a[2]/font")).click() and opening a new window and then it doesn't recognise the element driver.findElement(By.name("p_subj_code")).click(); – shinymoon Aug 14 '16 at 21:45
  • @ Saurabh Gaur , As I said earlier Element by name p_subj_code is in new window even I tried Window Handling This doesn't work :( – shinymoon Aug 14 '16 at 21:47
  • Thank-you guys for looking at my code , All of your efforts are much appreciated :) , I hope to get a way through with this tricky code. – shinymoon Aug 14 '16 at 21:48
  • Yippee......I manged to find the Element on the child window driver.findElement(By.name("p_subj_code")).click(); But after I execute this steps driver.findElement(By.name("p_subj_code")).click(); //(Opens a new page) driver.findElement(By.xpath(".//*[@id='majr_code_id']/option[2]")).click(); driver.findElement(By.xpath("//input[@id='p'_crse_numb']")).sendKeys("111"); driver.findElement(By.xpath("//input[@value='Find']")).click(); I could not access another Child Window ..... – shinymoon Aug 14 '16 at 23:52
  • I used statement If(!handles.isEmpty()){ for(string windowId:handles){ driver.switchTo().window(windowId);if(driver.getPageSource().contains("dedefault"))//"Description" is the name of the label of your text area { try{ WebElement SubCodeDropDown = driver.findElement(By.name("p_subj_code")); and after this another new child window will open where I have to select the year and confirm in the pop up window to copy the selected details. can I get any help please? on how to get cursor on another child window from the previous child window and then go to parent window? – shinymoon Aug 15 '16 at 01:40

1 Answers1

1

Tried this? : How to handle the new window in Selenium WebDriver using Java?

Community
  • 1
  • 1
ahly212
  • 96
  • 1
  • 9