找不到它位于框架内和网页附近的网页

时间:2017-11-10 12:54:48

标签: java selenium selenium-webdriver

元素在框架内的标记附近用**突出显示,但Java代码无法切换下面的框架是源代码和WebDriver代码。

这是源HTML:

<iframe id="Ifrm1510295770939" class="abs0 hw100" frameborder="0" scrolling="no" allowtransparency="true" src="/ecp/UsersGroups/ResourceMailboxes.slab?reqId=1510296120132&showhelp=false" title="resources - Microsoft Exchange" ldhdlr="1" cf="t">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en-US" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<body tabindex="-1" umc-topregion="true" onkeydown="if(typeof(HandleShortcutKeys) == 'function') {HandleShortcutKeys(event);}" role="application" scroll="no" aria-busy="false">
<noscript> <meta http-equiv="refresh" content="0;URL=/ecp/error.aspx?cause=noscripts" /> </noscript>
<form id="aspnetForm" class="hw100" role="form" onsubmit="javascript:return WebForm_OnSubmit();" action="./ResourceMailboxes.slab?reqId=1510296120132&showhelp=false" method="post">

我要查找的元素突出显示为:

  <div class="aspNetHidden">
a class="ToolBarButtonLnk" href="#" role="button" onclick="javascript:return false;" title="Search" aria-label="Search" aria-disabled="false">
  *<img class="ToolBarImage CommandSprite search-default" alt="Search" src="https://r1.res.office365.com/ecp/15.20.197.22/themes/default/clear1x1.gif" title="Search">
  <span class="ToolBarButtonImageAlter">Search</span>

enter image description here

我用过切换框架但没有工作。我在Selenium中写的Java代码:

 public class Verify {

    WebDriver driver;
    Actions ac1;



    public void Login1() throws InterruptedException
    {   


          driver = new FirefoxDriver();
          driver.manage().window().maximize();
    driver.get("https://outlook.office.com/ecp");

    driver.findElement(By.xpath("//input[@id='cred_userid_inputtext']")).sendKeys("BSL_Test1@c02.mwslab.net");
    driver.findElement(By.xpath("//input[@id='cred_password_inputtext']")).sendKeys("Xoraan*(987");
    //driver.findElement(By.xpath("//button[@id='cred_sign_in_button']")).click();
    Thread.sleep(2000);
    ac1 = new Actions(driver);
    ac1.sendKeys(Keys.RETURN).perform();
    }

    public void admincentre() throws InterruptedException
    {
        Thread.sleep(2000);
        driver.findElement(By.xpath("//a[@class='priNavLnk' and @index='1']")).click();
        driver.findElement(By.xpath(".//*[@id='Menu_Resources']/a")).click();
        //driver.findElement(By.xpath("//table[@id='ResultPanePlaceHolder_ctl00_ctl02_ctl01_ResourceMailboxesListView_contentTable']/tbody/tr[10]/td[1]")).click();
        Thread.sleep(5000); 
    }

    public void trial()throws InterruptedException {
        Thread.sleep(5000);
    //WebElement fra = driver.findElement(By.xpath("//iframe[@id='Ifrm1510207939402' and @title='resources - Microsoft Exchange']"));
    int count =driver.findElements(By.tagName("iframe")).size();
    System.out.println(count);



    driver.switchTo().frame("Ifrm1510207939402");
    driver.findElement(By.xpath("img[@class='ToolBarImage CommandSprite search-default']")).click();

    //List<WebElement> numberOfFrames=  driver.findElements(By.tagName("iframe"));
    //System.out.println("Total Number of iframes present are : "  +numberOfFrames.size()); 
    }

    @Test(priority=0)
    public void start() throws InterruptedException{
        Login1();
        admincentre();
    }

    @Test(priority=1)
    public void start1() throws InterruptedException{

        trial();
    }



    }

即使是暗示和明确的等待也没有工作。

4 个答案:

答案 0 :(得分:1)

iframe的ID始终更改。此外,您执行开关的方式:

driver.switchTo().frame("Ifrm1510207939402");

不正确。

在必须执行正确的开关之前:

        WebElement iframe= driver.findElement(By.xpath("//iframe[@title='resources - Microsoft Exchange']"));
        driver.switchTo().frame(iframe);

之后您可以搜索感兴趣的元素。

建议:不要使用thread.sleep。请务必使用explicit waits

整个代码:

        driver.get("https://outlook.office.com/ecp");
        driver.findElement(By.xpath("//input[@id='cred_userid_inputtext']")).sendKeys("user");
        driver.findElement(By.xpath("//input[@id='cred_password_inputtext']")).sendKeys("password");
        WebElement button = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.elementToBeClickable(By.id("cred_sign_in_button")));

        button.click();
        Actions ac1 = new Actions(driver);
        ac1.sendKeys(Keys.RETURN).perform();

        WebElement recipients = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='priNavLnk' and @index='1']")));

        recipients.click();

        WebElement resources = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.elementToBeClickable(By.id("Menu_Resources")));
        resources.click();

        WebElement iframe= driver.findElement(By.xpath("//iframe[@title='resources - Microsoft Exchange']"));
        driver.switchTo().frame(iframe);

        WebElement tt = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//form[@id='aspnetForm']/div[@id='wrpdv']/div[@id='ResultPanePlaceHolder_ctl00']/div/table/tbody/tr/td/div/div[@class='cttPane']/table/tbody/tr/td/div[@id='ResultPanePlaceHolder_ctl00_ctl02_ctl01_ResourceMailboxesListView']/div[@class='ToolBarContainer TopCtrl']/div[@id='ResultPanePlaceHolder_ctl00_ctl02_ctl01_ResourceMailboxesListView_ToolBar']/div[@class='ToolBarItem  ToolBarButton ToolBarButton InlineSearchBarCommand  EnabledToolBarItem']")));


        tt.click();


        /*to return to default content
          driver.switchTo().defaultContent();
        */

PS:

如果从示例中删除凭据,可能会更好。

答案 1 :(得分:0)

我在你的代码中看到了html中iFrame的另一个id。尝试使用此ID进行切换:

driver.switchTo().frame("Ifrm1510295770939");

答案 2 :(得分:0)

从您分享的HTML可以看出,id标记的<iframe>属性是动态的。因此,我们必须正确切换到框架并单击目标元素,如下所示:

WebDriverWait wait1 = new WebDriverWait(driver, 10);
wait1.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@id,'Ifrm') and contains(@title,'resources - Microsoft Exchange')]")));
driver.findElement(By.xpath("//img[@class='ToolBarImage CommandSprite search-default']/span[@class='ToolBarButtonImageAlter']")).click();

答案 3 :(得分:0)

尝试

Thread.sleep();

等待时间过长。