seleniumselenium-webdriverselenium-firefoxdriver

How to click a button created using <span> tag for Selenium webdriver?


I have span tag which looks like a button on html tag

    <span class="middle">Next</span>

I tried using

    xpath=driver.findElement(By.xpath(".//*[@id='modal-actions-panel']/div[2]/a/span/span/span")); // by considering fixed id as reference

Using absolute

xpath=driver.findElement(By.xpath("html/body/div[4]/div[2]/a/span/span/span"));  // took this from firebug

and Using

driver.findElement(By.cssSelector("span[class='middle']"));

No success!! It is throwing below exception :

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[contains(., \"Next\")]"} Command duration or timeout: 30.12 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

For all the ways I tried it is showing the same exception with change in selector details. Can someone please help me out in finding solution so that I can find Next button that is in span tag and click it.

Next button is in iFrame: Below is the part of html covering required span tag.

Next

I also tried with :

driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));

But throwing below error :

Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

Kindly let me know if needed something that I'm missing..


Solution

  • I think this element is inside a frame or iframe, if it is then you need to switch that frame or iframe before finding element as below :-

    driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
    WebElement el = driver.findElement(By.cssSelector("span.middle"));
    el.click();
    
    //Now after all your stuff done inside frame need to switch to default content 
    driver.switchTo().defaultContent();
    

    Edited1 :- If you are getting exception as element is not currently visible need to implement WebDriverWait to wait until element visible as below :-

     WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
    
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//span[@class = 'middle' and contains(text(), 'Next')]")));
    el.click();
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    

    Edited2 :- If unfortunately it's not getting visible try to click on it using JavascriptExecutor as below :-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
    
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//span[@class = 'middle' and contains(text(), 'Next')]")));
    
    //Now click using JavascriptExecutor
    ((JavascriptExecutor)driver).executeScript("arguments[0].click()" el); 
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();