Home > OS >  How to move Slider in an IFRAME that doesn't have a name/id?
How to move Slider in an IFRAME that doesn't have a name/id?

Time:01-23

enter image description here I am unable to move the slider in an IFRAME section: https://jqueryui.com/slider/

The IFRAME doesn't have a name or id on this page.

Code:

public class MovingSlider {
    public static void main(String[] args) 
    {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
        driver.get("https://jqueryui.com/slider/");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.switchTo().frame(1);
        WebElement slider = driver.findElement(By.xpath("//div[@id = 'slider']/span"));
        new Actions(driver).dragAndDropBy(slider, 400,0).click();
    }
}

CodePudding user response:

The issue is dom contains only one frame whose index is "0" and you tried to switch to index "1" frame which actually didn't exist.

Following is the code snippet that works for you.

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.get("https://jqueryui.com/slider/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Integer size = driver.findElements(By.tagName("iframe")).size();
System.out.println("Total iFrame is "   size);
driver.switchTo().frame(0);
WebElement slider = driver.findElement(By.xpath("//div[@id = 'slider']/span"));
// WebElement slider = driver.findElement(By.cssSelector("#slider > span"));
new Actions(driver).dragAndDropBy(slider, 400, 0).build().perform();
  •  Tags:  
  • Related