How to read canvas element attribute value using selenium?



  • I want to read canvas element value ("Answer:5015") but attribute value does not exist. Can you please let me know how to read attribute value?

    66dc3586-c81e-465c-a67d-5e30a1f89219-image.png



  • This code works, using Java and Edge browser:

    System.setProperty("webdriver.edge.driver", "msedgedriver.exe");
    
        WebDriver driver = new EdgeDriver();
        
        driver.get("https://the-internet.herokuapp.com/challenging_dom#edit");      
        
        String answer = new String();
        
        ArrayList<WebElement>scripts = new ArrayList<WebElement>((ArrayList<WebElement>) driver.findElements(By.tagName("script")));
        
        for(int i = 0; i < scripts.size(); i++) {
            
            String focusText = scripts.get(i).getAttribute("innerHTML");
            
            if(focusText.contains("canvas.strokeText")) {
    
                    answer = focusText.substring(focusText.indexOf("Answer"), focusText.indexOf("',"));
                    
                    break;
                    
            }
        }
        
        System.out.println(answer);
    


Suggested Topics