Java 如何在浏览器中验证pdf查看器

Java 如何在浏览器中验证pdf查看器,java,selenium,selenium-webdriver,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,我有一个“PDF视图”按钮,单击后,它会打开另一个新的浏览器窗口作为PDF查看器,我导航到第二个窗口。在这里,我试图验证是否有一些pdf内容显示在该页面上。我不需要验证任何pdf文本,只需要验证某些内容是否可用。 在这里,我尝试验证div标记(带有id)是否存在并显示在浏览器窗口中,并且由于某些原因,它没有标识该元素。请帮我找出我的代码有什么问题。在这里,任何其他需要验证的建议或解决方案都会有所帮助 Title of the browser is : Application V5 - PDF V

我有一个“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>

我曾面临过类似的问题。在我的例子中,元素的属性正在更改,我在xpath中使用了contains


尝试检查是否有帧,并使用fluent wait,我曾经遇到过类似的问题。在我的例子中,元素的属性正在更改,我在xpath中使用了contains


尝试检查是否存在帧,并使用fluent wait

getWindowHandles()返回的数组中的窗口顺序不可靠。单击打开新窗口的链接之前,请使用GetCurrentWindowHandle获取当前句柄。之后,检查数组中不是该句柄的值。。。这将是新窗口的句柄。顺便说一句,在截图之前,你可能还想睡一会儿。div可能在PDF加载之前就已经存在了。不幸的是,DOM对嵌入的内容了解不多。我尝试使用defaultwindowhandle,但它仍然无法识别第二个窗口元素。getWindowHandles()返回的数组中窗口的顺序不可靠。单击打开新窗口的链接之前,请使用GetCurrentWindowHandle获取当前句柄。之后,检查数组中不是该句柄的值。。。这将是新窗口的句柄。顺便说一句,在截图之前,你可能还想睡一会儿。div可能在PDF加载之前就已经存在了。不幸的是,DOM对嵌入的内容了解不多。我尝试使用defaultwindowhandle,但它仍然无法识别第二个窗口元素。
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();
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)