java Selenium:在同一选项卡中打开链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34378449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:49:58  来源:igfitidea点击:

Selenium: Open Link in Same Tab

javaseleniumselenium-webdriver

提问by ee clipse

I am clicking a link via Selenium webdriver and the link opens up a new windows - I want to force the link to open in the same window (and the same tab) is this possible?

我正在通过 Selenium webdriver 单击链接,该链接会打开一个新窗口 - 我想强制链接在同一窗口(和同一选项卡)中打开,这可能吗?

Most of the time this does not happen only with a specific link..

大多数情况下,这不仅仅发生在特定链接上。

Thanks

谢谢

回答by James

Before clicking the link update the link's targetproperty to selfand then click it.

单击链接之前,将链接的目标属性更新为self,然后单击它。

For updating the property please refer to this link.

有关更新属性,请参阅此链接

回答by Paras

See if the HTML code is like below, link will open in a different tab/windows depending upon the browser settings.

查看 HTML 代码是否如下所示,链接将根据浏览器设置在不同的选项卡/窗口中打开。

<a href = "#" target = "_blank">

When a Firefoxbrowser is launched via Selenium Webdriver, the default profile it launches by default has thisoption enabled. You can create a new firefox profile by disabling this option. In this case the link will open in the same firefox window.

Firefox通过 Selenium Webdriver 启动浏览器时,它默认启动的默认配置文件会启用选项。您可以通过禁用此选项来创建新的 Firefox 配置文件。在这种情况下,链接将在同一个 Firefox 窗口中打开。

In Chromedriver, new links open in the same window itself.

Chrome驱动程序中,新链接会在同一个窗口中打开。

You can force selenium webdriver to open a link in the same window but to open the link in same tab I don't think you can force it directly without injecting some Javascript. Using Javascriptyou can update the attribute targetto complete your requirement.

您可以强制 selenium webdriver 在同一窗口中打开链接,但要在同一选项卡中打开链接,我认为您不能直接强制它而不注入一些Javascript. 使用Javascript您可以更新属性target以完成您的要求。

If you want to inject Javascript you can use JavaScriptExecutorfrom Selenium Webdriver API.

如果你想注入 Javascript,你可以使用JavaScriptExecutorfrom Selenium Webdriver API.

((JavaScriptExecutor)driver).executeScript("document.getElementById('ID').setAttribute('target', 'self');")

回答by Prashanth Sams

For single-domelement

对于单 dom元素

@driver.execute_script("document.querySelector('your_css_element')[#{index}].setAttribute('target','_blank')")

For multi-domelements

对于多dom元素

 your_list_of_elements.each_with_index do |elem, index|
      @driver.execute_script("document.querySelectorAll('your_css_element')[#{index}].setAttribute('target','_blank')")
 end

(note: the above is a ruby snippet, apply the same in your preferred lang.)

注意:以上是一个 ruby​​ 片段,在您喜欢的语言中应用相同的内容。)

回答by Selenium Steve

WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.link.open_newwindow", 1);               
driver = new FirefoxDriver(options);