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>