Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

selenium - Getting time out exception when trying to identify the webelemnt

Below is the Code:

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");

Below is the DOM of the page, i am trying to click on New button which inside the iframe.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The xpath:

//div[@id='capTypePopup']

you used will find more than one elements,selenium will use the first find element, which may be not the one your expected.

From your screenshot, i noticed there are already two, I guess there should be another one ahead of the div your desired and it's not visible.

Use Dev tool to test the //div[@id='capTypePopup'], and confirm the first found div is visible or not.

If not visible, use more strict xpath which can find the div you desired. After that your problem should gone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...