首页 > 解决方案 > iframe 点击在 Chrome 中不起作用

问题描述

public void nmb_message() throws IOException, InterruptedException {

    WebDriver driver = Driverfactory.open("chrome", ".com");
    Logins login = new Logins();
    login.campaignqa();
    String actualTitle = driver.getTitle();
    String expectedTitle = "SmartFocus";

 
    (new Actions(driver)).dragAndDrop(element2, target2).perform();
    WebElement myDynamicElement2 = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.xpath( "//*[contains(text(), 'Add a picture')]")));
    myDynamicElement2.click();
    Thread.sleep(3000);
    driver.switchTo().frame(driver.findElement(By.cssSelector("#msg-editor-imagelib-iframe")));
    Thread.sleep(5000); 
    driver.findElement(By.xpath(".//*[@id='item-39571']/div/div[2]")).click(); Thread.sleep(15000L); driver.findElement(By.xpath( "//iframe[@class=contains(text(), 'Apply & close)]")).click(); }}

我已经能够切换到 iframe ,然后单击图像选择 [如图 1 所示],然后无法单击 div-preview 按钮 [如图 2 - 蓝色按钮],即“应用和关闭”。

使用 ""driver.findElement(By.xpath( "//iframe[@class=contains(text(), 'Apply & close)]")).click();" 或使用 Id 或 css 选择器。

图 1 图 2

任何想法 ?

<div id="msg-editor-imagelib" class="popup-container" style="visibility: visible; opacity: 1;">
<div class="popup-mask"></div>
<div class="popup confirm restrict">
<div class="popup-header">
<span class="popup-header-title left">Add image</span>
<span class="popup-header-close"></span>
</div>
<div class="popup-content">
<div class="switch-container">
<div class="image-popup-content">
</div>
<div class="options-panel-overlay open"></div>
<div class="options-panel-container empty open">
<div class="border-right"></div>
<div class="img-container">
<div class="notification info">You can select an image either by selecting an image from Smartfocus Image Library or enter a distant URL. Once selected, this panel will give you details like ALT tags and link.</div>
</div>
<div class="options-panel-container edition visible open">
<div class="border-right"></div>
<div class="image-preview-container">
<div class="custom-image-preview-placeholder onthefly-placeholder onthefly-placeholder--imghelper hidden">
<img id="image-popup-image-preview" src="http://p2itgtre.emv2.com/IL/5/4/8/1101011548/1735085964.jpg">
</div>
<div class="inputs-block">
<span class="image-label">1735085964</span>
<div id="image-source" class="input-header">
<span>Source</span>
<span class="source">Image Library</span>
</div>
<div class="input-header">
<span>Alternative tag</span>
</div>
<input id="alt-tag-input" type="text" value="">
<div id="alt-tag-helper" class="icon help cue sf-icon sf-icon--small"></div>
</div>
<div class="button-container">
<div id="image-popup-options-apply" class="submit blue popup-confirm">Apply & close</div>
</div>
</div>
</div>
</div>

标签: javaseleniumselenium-webdriver

解决方案


尝试使用此 Xpath 应用并关闭

.//*[@id='image-popup-options-apply']

推荐阅读