0
votes

This is the code I am trying to execute

public WebDriver createPart() {

    try {
        driver.findElement(By.id("username")).sendKeys("502409373");
        driver.findElement(By.id("password")).sendKeys("Magic14Magic");
        driver.findElement(By.id("submitFrmShared")).click();
        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
        Select dropCountry = new Select(driver.findElement(By.id("txtNewLocation")));
        dropCountry.selectByVisibleText("India");
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//button[@class='btn']/label")).click();
        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
        Thread.sleep(10000);
        driver.findElement(By.xpath("//li[@class='icon-button add']/span")).click();
        driver.findElement(By.xpath("//div[@id='ENCActions']/a/label")).click();    
        driver.findElement(By.xpath("//label[starts-with(text(),'Create Part...')]")).click();
        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
        String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
        String subWindowHandler = null; 
        Set<String> handles = driver.getWindowHandles(); // get all window handles
        Iterator<String> iterator = handles.iterator();
        while (iterator.hasNext()){
            subWindowHandler = iterator.next();
        }
        driver.switchTo().window(subWindowHandler); // switch to popup window
        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement element = driver.findElement(By.xpath("//select[@id='Type-Field']//following-sibling::div//div//input"));
        element.click();
        Thread.sleep(3000);
        element.sendKeys(Keys.BACK_SPACE);
        Thread.sleep(3000);
        element.sendKeys("Subassy");
        Thread.sleep(4000);
        driver.findElement(By.xpath("//div[@data-value='Subassy']")).click();
        driver.findElement(By.xpath("//span[text()='Description']//parent::td//following-sibling::td//textarea")).sendKeys("Testing");
        driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']//parent::select")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']")).click();
        driver.findElement(By.xpath("//a[text()='Done']")).click();
        driver.switchTo().window(parentWindowHandler);
        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver, 60);
        element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Part Details']")));
        element.click();

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return driver;

}

This is the html code of the element, I am unable to find.

<div class="tabcontent">
<a class="tablink" href="javascript:void(0);" title="Part Details" 
onclick="tvcTabPage_tabClicked(this,true)">Part Details</a>
<a href="javascript:void(0);" class="closetab" 
onclick="tvcTabPage_tabClosed(this)" title="Close Tab"></a>
</div>

This is the stack trace

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected 
condition failed: waiting for element to be clickable: By.xpath: 
//a[@title='Part Details'] (tried for 60 second(s) with 500 MILLISECONDS 
interval)

Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an 
element using By.xpath: //a[@title='Part Details']

Even though I have used wait statement. I am unable to locate the element. I have verified the xpath using chrome xpath. Please help me resolving my issue. Selenium version:3.3.1. The actual flow is, I am clicking a link in main page which results a popup consisting a form. After filling the form and clicking on done, popup gets closed automatically and main page gets refreshed and a new page is opened. Now, I am trying to click on the Part Details link in the new main page which is not working.

I have added this peice of code to find the right window handler.

subWindowHandler = null;
        Set<String> handles1 = driver.getWindowHandles(); // get all window handles
        iterator = handles1.iterator();
        while (iterator.hasNext()){
            subWindowHandler = iterator.next();
            driver.switchTo().window(subWindowHandler);
            element =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@title='Part Details']")));
            System.out.println("KK1");
        }

Giving me the output:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected 
condition failed: waiting for visibility of element located by By.xpath: 
//a[@title='Part Details'] (tried for 10 second(s) with 500 MILLISECONDS 
interval)

Here, required 'Part Details' element is located in a frame which I have included in code. But, i am unable to locate the element inside the frame. This is my modified code.

System.out.println(driver.switchTo().window(parentWindowHandler).getTitle());           
        driver.switchTo().frame("content");         
        driver.switchTo().frame("detailsDisplay");          
        driver.findElement(By.xpath("//a[@title='Part Details']")).click(); 

But I am facing this error.

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no 
such element: Unable to locate element: 
{"method":"xpath","selector":"//a[@title='Part Details']"}

I have attached my screenshot of the html code. HTML screenshot

4
Try using class driver.find_element_by_class_name("tablink") - bhupathi turaga
Also try: //*[@title="Part Details"] - Zhivko.Kostadinov
@Zhivko.Kostadinov , and what is the point? Target element is, obviously, a link - there is no need in using wild-card for tag name - Andersson
So in the original question you wanted us to help you in solving element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Part Details']"))); after driver.switchTo().window(parentWindowHandler); Now, as per your question update you wanted us to help you in solving element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@title='Part Details']"))); after driver.switchTo().window(subWindowHandler); - DebanjanB
Those 2 are suggestions posted here. Either way it is not working. I think I am unable to find the right window handler after the page gets refreshed. - kiran kumar

4 Answers

1
votes

Change visibilityOfElementLocated instead of elementToBeClickable. You can directly find the webeelement, and then click on it as shown below:

element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@title='Part Details']")));
0
votes

To invoke click() on the link with text as Part Details you need to induce WebDriverWait and invoke click() as follows :

  • linkText :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Part Details"))).click();
    
  • cssSelector :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.tablink[title='Part Details']"))).click();
    
  • xpath :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='tablink' and @title='Part Details']"))).click();
    
0
votes

your parentWindowHandler should not be correct, you fetch it at wrong time point.

Try move below code line to the first line in the try block

try {
   String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
0
votes

Once switched to parent window, try to refresh the page then find the element as given below. It may solve your issue.

    driver.switchTo().window(parentWindowHandler);
    driver.navigate().refresh();

    driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
    driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
    wait = new WebDriverWait(driver, 60);
    element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Part Details']")));
    element.click();