元素存在于一个框架中。代码识别框架,但不识别该框架中存在的元素。
<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
答案 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替换。
或强>
如果有多个元素具有相同的selector
或xpath
,您可以尝试Implicit Wait和findElements
的组合......
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List <WebElement> elements = driver.findElements(By.xpath("(//div[@class='tab-btns']/ul/li)["+2+"]"));