使用带有 Java 的 Selenium WebDriver 切换选项卡

使用带有 Java 的 Selenium WebDriver 切换选项卡

问题描述:

在 JAVA 中使用 Selenium WebDriver.我正在尝试自动化一个功能,我必须打开一个新选项卡在那里做一些操作并返回上一个选项卡(父).我使用了开关手柄,但它不起作用.一件奇怪的事情是这两个选项卡具有相同的窗口句柄,因此我无法在选项卡之间切换.

Using Selenium WebDriver with JAVA. I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). I used switch handle but it's not working. And one strange thing the two tabs are having same window handle due to which I am not able to switch between tabs.

但是,当我尝试使用不同的 Firefox 窗口时,它可以工作,但对于选项卡却不起作用.

However when I am trying with different Firefox windows it works, but for tab it's not working.

请帮帮我.如何切换标签页.或者如何在不使用窗口句柄的情况下切换选项卡,因为在我的情况下,窗口句柄与两个选项卡相同.

Please help me. How can I switch tabs. OR how can I switch tabs without using window handle as window handle is same of both tabs in my case.

(我观察到当你在同一个窗口中打开不同的标签时,窗口句柄保持不变)

(I have observed that when you open different tabs in same window, window handle remains same)

    psdbComponent.clickDocumentLink();
    ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs2.get(1));
    driver.close();
    driver.switchTo().window(tabs2.get(0));

这段代码非常适合我.试试看.在您想在新标签上执行某些操作之前,您总是需要将驱动程序切换到新标签.

This code perfectly worked for me. Try it out. You always need to switch your driver to new tab, before you want to do something on new tab.