Unable to click a button inside iframe which itself is inside shadow root in Selenium WebDriver using Java

Viewed 31
driver.get("https://thesportstak.com/");
    
    Thread.sleep(4000);
    WebElement scrollStory = driver.findElement(By.xpath("//div[@id='story3']"));
    

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
    wait.until(ExpectedConditions.visibilityOf(scrollStory));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView(true);", scrollStory);

    List<WebElement> homeStories = driver.findElements(By.xpath("//div[@class=\"mainDiv \"]"));
    List<WebElement> homeStoriesptag = driver.findElements(By.xpath("//div[@class=\"mainDiv \"]//p"));


    for (WebElement homeStoryp : homeStoriesptag) {
        System.out.println("getting home story p tag " + homeStoryp);
        System.out.println("Text of home story " + homeStoryp.getText());
    }

    WebElement firstStory =homeStories.get(3);

    firstStory.click();
    Thread.sleep(3000);

    WebElement shadow = driver.findElement(By
            .cssSelector("div[class=\"i-amphtml-fill-content i-amphtml-story-player-shadow-root-intermediary\"]"));
    // This Element is inside single shadow DOM.

    SearchContext shadowroot = shadow.getShadowRoot();
    Thread.sleep(1000);
    WebElement iframe = shadowroot.findElement(By.cssSelector(" div:nth-child(1) > iframe:nth-child(3)"));
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(iframe));
    
    
    List<WebElement> please = driver.findElements(By.xpath("//div[@class=\"letterbox\"]//p"));
    System.out.println(please);
    
    WebElement nextbutton =driver.findElement(By.cssSelector("button[aria-label='Next page']"));
    
    for(WebElement letterstory : please) {
        wait.until(ExpectedConditions.attributeToBeNotEmpty(letterstory,"class"));
        wait.until(ExpectedConditions.elementToBeClickable(nextbutton));
    
    ***nextbutton.click();*** ---->>> 

I am trying to click the above button. Next button.click. I have tried both xpath and css selector to find the button.

Also, I can only fetch the text from the first story above, the rest elemenets are present, but no text can be fetched from that. the p tag and the texts are present in the DOM and can be seen in the first story itself. last line.

Kindly help regarding the same.

        System.out.println("stories element "+ letterstory);
        ***System.out.println("stories inside "+letterstory.getText());***
    }
0 Answers
Related