【问题标题】:How to switchTo a frame when the body tag is hidden by style="overflow: hidden;"当body标签被style="overflow:hidden;"隐藏时如何切换到框架
【发布时间】:2019-03-26 18:48:03
【问题描述】:

在 PeopleSoft 页面中,如果我单击查找会弹出一个新小部件,我需要选择其中一个选项。

DOM 的设计方式在 body 标记中有“style="overflow: hidden;"”,通过下面的 xpath,我可以在 google chrome 中识别框架,但是,我无法切换到框架并点击我需要选择的选项

iframe 的 HTML:

<iframe frameborder="0" id="ptModFrame_2" name="ptModFrame_2" src="https://*******:8560/psc/umcssi2/EMPLOYEE/SA/c/SSR_PROG_ENRL.SSR_APT_REQ_RUNCNT.GBL?ICType=Panel&amp;ICElementNum=0&amp;ICStateNum=7&amp;ICResubmit=1&amp;ICAJAX=1&amp;" style="width: 514px; height: 350px;"></iframe>

我尝试使用下面的 xpath 切换到框架:

切换到框架的Xpath://div[@id='pt_modals']/div[2]/div/div[2]/iframe[contains(@src,'https://*******')]

Xpath切换后选择选项:

driver.findElement(By.xpath("//table/tbody/tr[4]/td[1]")).click();

注意:我也试过 javascript 执行器。

js.executeScript("arguments[0].click();",driver.findElement(By.xpath("//table/tbody/tr[4]/td[1]")));

我刚刚构建了同一个 DOM,

<body class="PSPAGE" id="ptifrmtemplate" style="overflow: hidden;"><div id="ptpopupmask" style="display: none;">&nbsp;</div>

我希望它应该点击,但它没有首先切换到框架。

【问题讨论】:

  • 与框架和代码共享 html
  • 这个DOM很大,可以分享一下格式
  • 您不需要共享整个 DOM,也许覆盖 &lt;iframe&gt; / &lt;frame&gt; 标记和所需元素及其父标记的 HTML 就足够了。

标签: java selenium selenium-webdriver iframe webdriverwait


【解决方案1】:

要切换到所需的&lt;iframe&gt;,因此您必须诱导 WebDriverWait 以使所需的 框架可用并切换到它,您可以使用任一 @ 987654321@:

  • xpath

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@id, 'ptModFrame_') and contains(@src, 'psc/umcssi2/EMPLOYEE/SA/c/SSR_PROG_ENRL.SSR_APT_REQ_RUNCNT.GBL')]")));
    
  • cssSelector

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='ptModFrame_'][src*='psc/umcssi2/EMPLOYEE/SA/c/SSR_PROG_ENRL.SSR_APT_REQ_RUNCNT.GBL']")));
    

【讨论】:

    【解决方案2】:

    尝试使用WebDriverWait等待帧

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("ptModFrame_2")));
    
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//table/tbody/tr[4]/td[1]"))).click();
    

    【讨论】:

    • 谢谢回复,切换到默认内容后也可以,driver.switchTo().defaultContent();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多