我试图单击Yahoomail登录页面下可用的亚马逊链接,但出现错误。
System.setProperty("webdriver.ie.driver", "C:/Users/Madankumar/Desktop/IE Driver/IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
driver.get("https://login.yahoo.com/?.src=ym&.intl=us&.lang=en-US&.done=https%3a//mail.yahoo.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
String mainhandle=driver.getWindowHandle();
System.out.println(mainhandle);
driver.switchTo().frame(1);
WebElement link1=driver.findElement(By.xpath("//div[@id='pagedeck']/div/div/gwd-taparea"));
Actions action1=new Actions(driver);
action1.moveToElement(link1).click().perform();
Thread.sleep(3000);
for(String childhandle:driver.getWindowHandles())
{
driver.switchTo().window(childhandle);
}
HTML参考
<iframe id="html5_container" marginwidth="0" marginheight="0" hspace="0" vspace="0" scrolling="no" name="%7B%22frameId%22:%22html5_container%22,%22tracking%22:%7B%22rB%22:%22https://beap-bc.yahoo.com/yc/bv=1.0.0&bs=(17dsoelop(gid$GSbAmAAAAACEuz2xzpiJvRVKatCnTViCMyQADie3,st$1484927780927511,si$13061,sp$150272956,ct$25,ybx$hx4aXMrqdiyPjj_DZ0tJkw,lng$en-in,cr$1457286061,v$2.0,aid$aBcNzmoKntc-,bi$331932061,mme$4457353829410934013,r$1,yoo$1,agp$390870949,ap$RICH))/*%22,%22r0%22:%22https://beap-bc.yahoo.com/yc/YnY9MS4wLjAmYnM9KDE3ZHIxMW1mNyhnaWQkR1NiQW1BQUFBQUNFdXoyeHpwaUp2UlZLYXRDblRWaUNNeVFBRGllMyxzdCQxNDg0OTI3Nz...zM1MzgyOTQxMDkzNDAxMyxyJDIseW9vJDEsYWdwJDM5MDg3MDk0OSxhcCRSSUNIKSk/1/*%22,%22z1%22:%22https://beap-bc.yahoo.com/reg_ci?bv=1.0.0&bs=(14006bjie(gid$GSbAmAAAAACEuz2xzpiJvRVKatCnTViCMyQADie3,si$13061,sp$150272956,st$1484927780927511,bi$331932061,cr$1457286061,cpcv$0,v$2.0))&al=(as$11vc4pqjd,aid$aBcNzmoKntc-,ct$25,id(%7Bbeap_client_event%7D))%22,%22cb%22:%221484927780.760654%22,%22trackUnique%22:true%7D,%22exitUrls%22:%7B%22clickTag%22:%22https://s.yimg.com/cv/ae/20jan2017/amz/Amazon_ML_20_Jan_RB.html%22%7D%7D" src="https://s.yimg.com/cv/ae/01725370/index.html" style="width: 100%; height: 100%; position: absolute; top: 0px; left: 0px; display: block; z-index: 2;" frameborder="0">
<!DOCTYPE html>
<html>
<head>
<body style="">
<gwd-doubleclick id="gwd-ad" polite-load="">
<gwd-metric-configuration>
<div id="pagedeck" class="gwd-page-container gwd-pagedeck" is="gwd-pagedeck">
<div id="page1" class="gwd-page-wrapper gwd-page-size gwd-lightbox gwd-page gwd-play-animation" is="gwd-page" data-gwd-width="600px" data-gwd-height="450px" style="">
<div class="gwd-page-content gwd-page-size">
<img id="bg" class="gwd-img-1f04" is="gwd-image" source="bg.png" src="bg.png">
<img id="unit" class="gwd-img-1u93" is="gwd-image" source="unit_600x450.png" src="unit_600x450.png">
<img id="bigsaving" class="gwd-img-w5t8 gwd-gen-15zdgwdanimation" is="gwd-image" source="t1.png" src="t1.png">
<img id="onbigbrands" class="gwd-img-1q75 gwd-gen-1dqjgwdanimation" is="gwd-image" source="2.png" src="2.png">
<img id="shopnow" class="gwd-img-16k2 gwd-gen-1ssegwdanimation" is="gwd-image" source="shop-now.png" src="shop-now.png">
<gwd-taparea id="gwd-taparea_1" class="gwd-taparea-e13e"></gwd-taparea>
我正在尝试切换到图像
Eclipse中的错误消息
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //div[@id='pagedeck']/div/div/gwd-taparea (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 4.13 seconds
最佳答案
您尝试使用iframe
切换到driver.switchTo().frame(1);
,这意味着切换到第二个iframe
元素。尝试
driver.switchTo().frame(0);
WebElement link1=driver.findElement(By.xpath('//gwd-taparea[@id="gwd-taparea_1"]'));
要么
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("html5_container");
WebElement tapArea = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//gwd-taparea[@id="gwd-taparea_1"]')));
关于java - 使用Selenium WebDriver切换到iframe后无法单击它,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41768754/