How to make Firefox open all links opened via WebDriver in the same window?

后端 未结 6 1586
礼貌的吻别
礼貌的吻别 2021-01-04 10:23

I want to open all links in the same window instead in new window. I tried

profile.setPreference(\"browser.link.open_newwindow\", 1)

but th

6条回答
  •  花落未央
    2021-01-04 11:04

    I've found a workaround!

    JavascriptExecutor js = (JavascriptExecutor) driver;
    String script = "document.getElementById('yourFormOrAnchorId').target=''";
    js.executeScript(script);
    

    After that you can select your anchor or any of the form elements and click or submit it. The target page will open in the same tab.

    This basically changes the current HTML page so that anchors and forms don't force the browser to open new tabs or windows. For testing this might be suboptimal, but it simplifies the writing of tests a lot.

提交回复
热议问题