java - 无法找到位于框架内且靠近网络表的网络

标签 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();
    }



    }

尝试过隐式和显式等待都不起作用。

最佳答案

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:

如果您从示例中删除凭据,也许会更好。

关于java - 无法找到位于框架内且靠近网络表的网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47223275/

相关文章:

python - 使用 selenium 更改表中的值

java - 使用 maven-assembly-plugin 创建两个可执行的 jar

java - Double.toHexString() 语法 - 它来自哪里?

selenium - 如何与 gradle 并行运行 Serenity Web 测试?

php - 用 mink 操纵标签

jenkins - 错误 : Could not find or load main class org. testng.TestNG

selenium - headless 谷歌浏览器 : How to prevent sites to know whether their window is focused or not

用于测试自动化框架的Java空指针异常

java - 这个递归示例是如何工作的?我不断收到堆栈溢出错误

java - 选择要使用 Wicket 7 DropDownChoice 显示的模型属性