无法找到框架中存在的元素

时间:2014-01-04 15:24:42

标签: java iframe selenium-webdriver

我需要的

元素存在于一个框架中。代码识别框架,但不识别该框架中存在的元素。

<div id="cod-info" class="lightbox infobox">
<div id="returns-info" class="lightbox infobox">
<div id="lb-coupon-widget" class="lightbox">
<div id="lb-login" class="lightbox loginbox loginsignupV2" data-signupsplashpageload="2">
<div id="lightbox-shim" class="lightbox-shim" style="display: block;"></div>
<iframe id="mklogin-iframe" src="javascript:void(0)" name="mklogin-iframe" style="position: absolute; height: 0px; top: -100px;">-----> Frame
<div class="myntra-tooltip">
<div id="lb-sizechart" class="lightbox lb-sizechart" style="display: block;">
<div class="mod" style="min-height: 478px; margin-top: 20px; margin-bottom: 20px;">
<div class="close"></div>
<div class="loading" style="display: none;"></div>
<div class="hd">
<div class="bd mk-cf">
<div>
<div id="tab-list" class="mk-f-left lft-cont">
<div class="tab-btns">----> Want to locate element present in this div
Code : driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='mklogin-iframe']")));---- to switch to frame

driver.findElement(By.xpath("(//div[@class='tab-btns']/ul/li)["+2+"]"));---> to locate element.

It is showing unable to locate element

1 个答案:

答案 0 :(得分:0)

我认为您不是在等待元素出现,您可以尝试使用Explicit Wait ...

WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("selector_of_element")));

注意:'cssSelector'可以根据您的方便用xpath,id,name,className..etc替换。

如果有多个元素具有相同的selectorxpath,您可以尝试Implicit WaitfindElements的组合......

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List <WebElement> elements = driver.findElements(By.xpath("(//div[@class='tab-btns']/ul/li)["+2+"]"));