2017-01-23 26 views
-2

我無法在窗口的iframe之間切換。我想在網頁的頂部窗口中選擇一個iframe。切換到Selenium-Java中的iframes

頁面的鏈接是: http://way2automation.com/way2auto_jquery/dropdown.php#example-1-tab-1

enter image description here

我可以找到兩個I幀,但不能切換到I幀。每個iframe都有自己的下拉列表,我需要從中選擇元素。

我試過使用driver.switchto(),但它不識別iframes。

我的代碼是:

public void SimpleDropDown() throws InterruptedException { 
     dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
//  dr.findElement(By.xpath("//a[text()='Select Country']")).click(); 
//  dr.switchTo().frame(dr.findElement(By.xpath("//div[@id='example-1-tab-1']//iframe"))); 
     Select dropdown = new Select(dr.findElement(By.xpath("html/body/select"))); 
     dropdown.selectByVisibleText("India"); 
     System.out.println(dropdown.getFirstSelectedOption().getText()); 
    } 

    public void comboBox() { 
     dr.switchTo().frame(2); 
     dr.findElement(By.xpath("//a[text()='Enter Country']")).click(); 
     Select dropdown = new Select(dr.findElement(By.xpath("//select[@id='combobox']"))); 
     dropdown.selectByVisibleText("Portugal"); 
+0

使用'幀索引對於'第一幀' – NarendraR

+0

目標頁面需要註冊,所以你最好提供適當的'HTML'代碼。在切換到下一個iframe之前,您還需要'driver.switchTo()。defaultContent();' – Andersson

回答

0

切換到幀的起始指針爲0的作品,但不是切換到默認框架,應該使用SWITCHTO()parentFrame(),因爲它可以追溯到頂部窗口(。只有當你不使用嵌套幀)。

0

嘗試執行此代碼。

WebElement iframe = driver.findElement(By.tagName("iframe")); 
driver.switchTo().frame(iframe);        //Move inside to the frame. 
WebElement body = driver.findElement(By.tagName("body")); 
body.click(); 
driver.findElement(By.xpath("//a[text()='Enter Country']")).click(); 
Select dropdown = new Select(driver.findElement(By.xpath("//select[@id='combobox']"))); 
dropdown.selectByVisibleText("Portugal"); 
driver.switchTo().defaultContent();       //Move outside to the frame. 
+0

如果問題解決了,請將其標記爲Accepted。所以它也會對其他用戶有所幫助。謝謝:) –

0

您的網站在同一頁面顯示2 iFrames。所以,選擇下拉之前,你需要切換到frame爲您的下拉正在iframe -

driver.switchTo().frame(0); 
    Select select = new Select(driver.findElement(By.tagName("select"))); 
    select.selectByValue("Angola"); 

一旦你做出了選擇,需要使用出來的框架 -

driver.switchTo().defaultContent(); 

然後導航其他選項卡,然後再你的元素是iframe下,所以需要與0`做同樣的動作

driver.findElement(By.xpath("//li/a[text()='Enter Country']")).click(); 
    driver.switchTo().frame(1); 
    driver.findElement(By.xpath("//span[@class='custom-combobox']/input")).sendKeys("India"); 
    driver.switchTo().defaultContent(); 
+0

這就是第一個答案說的! – Vishal

+0

需要等待6小時才能接受。 – Vishal