Search code examples
javaseleniumiframexpathnosuchelementexception

How to get to the element through JS in iframe? Selenium, Java


So what we have - there's an iFrame on page which shows a preview of previously generated data. I need to get element with text and then get CSS value from it.

Take a look at the DOM here:

example of the page xpath to this element looks right to needed element (God save Chrome DevTools!). But script cannot detect this element.

What I've did: 1 - switched to Iframe where my element is located - successfully.(no more iframe inside this iframe as per screenshot). 2 - tried to find element - NoSuchElementException.

Probably, such issue appears because of this #document thing ?If so - how do I can solve it and get to needed element with script ?


Solution

  • may be you need to wait till the frame to be loaded as given below.

    WebDriverWait wait=new WebDriverWait(driver, 90);
    driver=wait.untill(ExpectedConditions.frameToBeAvailableAndSwitchToIt("previewFrame");
    
    WebElement element=driver.findElement(By.TagName("em"));
    String fontStyle=element.getCssValue("font-sytle");
    System.out.println(fontStyle);