在尝试识别webelemnt时获取超时异常

时间:2017-09-12 19:22:29

标签: selenium selenium-webdriver webdriver

以下是代码:

WebElement Username=d1.findElement(By.xpath("//*[@id='username']"));
Username.sendKeys("aadmin");

WebElement Password=d1.findElement(By.xpath("//*[@id='login_form']/tbody/tr/td/table/tbody/tr[12]/td[2]/input"));
Password.sendKeys("admin");

WebElement signin=d1.findElement(By.xpath("//*[@id='submit_']"));
signin.click();

System.out.println("User admin has logged in  "+ d1.getTitle());
w1.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='spaces-menu']/ul/li[1]"))).click();

System.out.println("User Admin clicks on Record button");

//Thread.sleep(5000);
d1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//Switching the control
int size2=d1.findElements(By.tagName("iframe")).size();
System.out.println("iframe size is---" + size2);

WebDriverWait w2= new WebDriverWait (d1, 15);       
//w2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(0));
w2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.tagName("iframe")));

System.out.println("Page title is     "+d1.getTitle());

//d1.switchTo().frame("iframe-page-container");

//d1.switchTo().frame(d1.findElement(By.tagName("iframe")));

WebDriverWait w3= new WebDriverWait (d1,30);

WebElement New=w3.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='capTypePopup']")));
    //WebElement New=w3.until(ExpectedConditions.visibilityOfElementLocated(By.className("menu-middle-normal-button")));
    //WebElement New=w3.until(ExpectedConditions.visibilityOfElementLocated(By.id("menuButtonContain-6")));
    //New.click();

    //d1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    //JavascriptExecutor js = (JavascriptExecutor) d1;
    //WebElement element = d1.findElement(By.id("menuButtonContain-6"));

    //js.executeScript("arguments[0].setAttribute('type', '')",element);

    //System.out.println(d1.findElement(By.id("menuButtonContain-6")).getAttribute("value"));


    //Actions a1= new Actions(d1);
    //a1.moveToElement(New).click(New).build().perform();

    //d1.findElement(By.xpath("//*[@id='tr_menubar']/td"));
    //d1.findElement(By.xpath("//*[@id='capTypePopup']")).click();
    //w1.until(ExpectedConditions.elementToBeClickable(By.className("portlet-menu-item"))).click();
    //w2.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='capTypePopup']/font"))).click();
    System.out.println("Click on the new button");

下面是页面的DOM,我试图点击iframe里面的New按钮。

enter image description here

1 个答案:

答案 0 :(得分:0)

xpath:

//div[@id='capTypePopup']

你使用过会发现多个元素,selenium会使用第一个find元素,这可能不是你预期的那个。

从你的屏幕截图中,我注意到已经有两个了,我想在你想要的div之前应该有另一个,这是不可见的。

使用Dev工具测试//div[@id='capTypePopup'],并确认第一个找到的div是否可见。

如果看不到,请使用更严格的xpath,它可以找到您想要的div。之后你的问题就不复存在了。

相关问题