简体   繁体   English

无法在 Java Selenium 无头浏览器中的新 window 中捕获文本

[英]Unable to capture text inside new window in Java Selenium Headless browser

I'm running all my test suites on Jenkins which is deployed on AWS EC2 instance.我在部署在 AWS EC2 实例上的 Jenkins 上运行我的所有测试套件。 There is a scenario where when I click on a button, new small window opens up and I'm doing assertion for the text visible inside the small newly opened window.有一种情况是,当我单击一个按钮时,新的小 window 打开,我正在对新打开的小 window 中可见的文本进行断言。 But my tests are failing when I run using Headless mode.但是当我使用无头模式运行时,我的测试失败了。 But, same scripts works fine when I run scripts locally without opting for headless browser.但是,当我在本地运行脚本而不选择无头浏览器时,相同的脚本可以正常工作。

The issue here is the scripts are failing because of headless browser since it's unable to capture text inside small window which has opened after click of button.这里的问题是脚本由于无头浏览器而失败,因为它无法在单击按钮后打开的小 window 内捕获文本。 This class is extending InitiateDriver class which explained.这个 class 正在扩展解释的 InitiateDriver class。 Below class is trying to fetch text which is visible inside the new window which just opened after clicked on SignInWithGSuiteSSOClick() button. class 下面试图获取在新的 window 中可见的文本,该文本在单击 SignInWithGSuiteSSOClick() 按钮后刚刚打开。

Here is the code:
// click on a button
GSuiteobject.SignInWithGSuiteSSOClick().click(); 
String winHandleBefore = driver.getWindowHandle();
// Here trying to capture text inside new window opened up basically gmail window to enter email 
String signInHeader = GSuiteobject.GsuiteSignInHeader().getText();
Assert.assertEquals(signInHeader, "Sign in");
GSuiteobject.GsuiteEmail().sendKeys("example@gmail.com");
            

InitiateDriver.java启动驱动程序.java

 ChromeOptions options = new ChromeOptions();
 options.addArguments("--headless");
 options.addArguments("--window-size=1920, 1080");
 options.addArguments("--disable-gpu");
 driver = new ChromeDriver(options);

The same code works in browser mode but not in headless.相同的代码在浏览器模式下有效,但在无头模式下无效。 But the driver will be initialized but it fails only while capturing text.但是驱动程序将被初始化,但仅在捕获文本时才会失败。 Please help me out I'm stuck here and unable to execute it on Jenkins as a headless browser.请帮帮我,我被困在这里,无法在 Jenkins 作为无头浏览器上执行它。

Headless chrome browser's gmail UI will be different from actual gmail UI(latest).无头 chrome 浏览器的 gmail UI 将不同于实际的 gmail UI(最新)。 Hence it fails when we run headless chrome for automating gmail login since xpaths will differ.因此,当我们运行无头 chrome 来自动登录 gmail 时,它会失败,因为 xpath 会有所不同。 We can validate that by taking Screenshot by running on headless and normal browser.我们可以通过在无头和普通浏览器上运行截屏来验证这一点。

I suggest to take screenshot using both headless and normal browser.我建议使用无头浏览器和普通浏览器截取屏幕截图。 To check on xpaths for headless we can try driver.getPageSource() method.要检查 xpaths 是否无头,我们可以尝试 driver.getPageSource() 方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 Selenium Webdriver java 在 Linux 机器上运行 Headless Chrome 浏览器 - Unable to run Headless Chrome Browser on Linux Machine using Selenium Webdriver java 如何使用Java在Selenium2中捕获屏幕截图,需要在最小化浏览器时捕获带有其父窗口(浏览器)的弹出窗口 - How to capture Screenshot in Selenium2 using Java, need to capture popup window with its parent window(Browser) when browser is minimized Selenium (Java) - Chrome Headless - 无法上传文件 - Selenium (Java) - Chrome Headless - Unable to upload the file 使用Selenium和JAVA访问在新的浏览器窗口中打开的pdf - Accessing a pdf opened in a new browser window with Selenium and JAVA 在新标签页中切换时无法在 Chrome Headless 模式下捕获屏幕截图 - Unable to capture screenshot in Chrome Headless mode while switched in new tab 在IE上使用Java中的Selenium WebDriver无法获得新的窗口句柄 - Unable to get new window handle with Selenium WebDriver in Java on IE 无法切换到主窗口内的浏览器窗口。(Selenium Webdriver) - Unable to switch to Browser Window which is inside the main window.(Selenium Webdriver) 如何使用Selenium Java中的机器人类在无头浏览器中上传文件 - How to upload file in headless browser using robot class in selenium java 无法捕获错误消息 - selenium java junit - Unable to capture error message - selenium java junit 无法使用Java Selenium Webdriver在Xvfb中无头鼠标悬停 - unable to mouseover in headless with Xvfb using java selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM