1

我有一个“PDF 查看”按钮,单击后,它会打开另一个新的浏览器窗口作为 pdf 查看器,然后我导航到第二个窗口。在这里,我试图验证该页面上是否显示了某些 pdf 内容。我不需要验证任何 pdf 文本,只需要验证某些内容是否可用。在这里,我尝试验证 div 标签(带有 id)是否存在并显示在浏览器窗口上,并且由于某种原因它没有识别该元素。请帮助我解决我的代码有什么问题。任何其他要验证的建议或解决方案在这里都会有所帮助。

Title of the browser is : Application V5 - PDF Viewer
URL of the browser is : https://sample.com/preferences/pdfviewer.jsp

相应的html代码是

<div id="content">
    <embed id="plugin" type="application/x-google-chrome-pdf" src="https://sample.com/servlet/pdfviewer" stream-url="chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/23576f4e-363a-4f60-a63a-b770beb66c41" headers="Cache-Control: private
Connection: Keep-Alive
Content-Disposition: inline; filename=image.pdf
Content-Length: 23721
Content-Type: application/pdf
Date: Thu, 09 Jan 2020 18:53:13 GMT
Expires: Thu, 09 Jan 2020 18:53:18 GMT
Keep-Alive: timeout=5, max=98
Server: Apache
" background-color="0xFF525659" top-toolbar-height="56" javascript="allow" top-level-url="undefined">
</div>

我的脚本有

WebElement pdfViewButton = browser.findElement(By.id("frmResults:btnPdfView"));
pdfViewButton.click();

WebDriverWait wait = new WebDriverWait(browser, 60);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));

Set<String> AllWindowHandles = browser.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
scenario.write("Currently in Parent Window = " + AllWindowHandles.toArray()[0]);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
scenario.write(String.valueOf(AllWindowHandles.size()));

String window2 = (String) AllWindowHandles.toArray()[1];
scenario.write("Switching to Child (Viewer) window = " + AllWindowHandles.toArray()[1]);
browser.switchTo().window(window2);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("content")));
Screenshot.takeScreenshot(BaseSteps.scenario, browser);
WebElement pdfViewer = browser.findElement(By.id("content"));
assertThat(pdfViewer.isDisplayed())
    .overridingErrorMessage("The pdf viewer window is launched, but there is no content is displayed.")
    .isTrue();

browser.switchTo().window(window2).close();

这是错误...(错误出现在“wait.until”行...

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.id: content (tried for 60 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at Utilities.Common.viewTheContentsWithPDFViewer(Common.java:268)
    at Steps.Steps.Verify_the_pdfviewer_is_launching_with_data_content_for_that_item(Steps.java:157)
    at ✽.And Verify the pdfviewer is launching with data content for that item(Returns.feature:19)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#content"}
  (Session info: chrome=78.0.3904.87)
4

1 回答 1

0

我遇到过类似的问题。在我的情况下,元素的属性正在改变,我在 xpath 中使用了 contains。

尝试检查是否有框架并使用流利的等待

于 2020-01-10T08:43:17.037 回答