Open link in new tab


no mice here

I need to open a link <a>in a new tab/window 's tab, do something there and then close it.

html code:

<body>
    <a id="uniqueid" href="somelink">I need to open this in a new tab</a>
</body>

python code:

from selenium import webdriver

driver = webdriver.Firefox(executable_path="geckodriver")

driver.get("url")
element = driver.find_element_by_id("uniqueid")

# I need to do this somehow
element.open_on_new_tab()
Joao

First, you want to get the url from "uniqueid"the element :

url = element.get_attribute('href')

Then you get that page, switch to it with Ctrl-TAB , and do something there:

# Save current window
w0 = drv.window_handles[0]

# Create new window, make it the active tab
drv.execute_script("window.open('" + url + "');")
drv.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
sleep(3)
w1 = drv.window_handles[1]

# Change to the new window
drv.switch_to.window(w1)

# Do something here

After that, you can w0switch back to :

drv.switch_to.window(w0)

Related


Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

pandoc: open link in new tab

singles According to this answer , it should be possible to use the following syntax [link](url){:target="_blank"} When used, links can be opened in a new tab pandoc. However, this option does not seem to be supported as it is not recognized when converting t

Button to open link in new tab

Alex Hawking I'm building a website and I want to open a new tab at a specific address when a button is clicked. Here is the html of my button: <button id="AsDownload" onclick="AsDownload();">Download</button> Here is my javascript: function AsDownload(){

Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

pandoc: open link in new tab

singles According to this answer , it should be possible to use the following syntax [link](url){:target="_blank"} When used, links can be opened in a new tab pandoc. However, this option does not seem to be supported as it is not recognized when converting t

Button to open link in new tab

Alex Hawking I'm building a website and I want to open a new tab at a specific address when a button is clicked. Here is the html of my button: <button id="AsDownload" onclick="AsDownload();">Download</button> Here is my javascript: function AsDownload(){

phpBB open link in new tab

Adam I have a phpBB board 3 with prosilver style. When the user clicks the link in the post, the browser will open it in the same tab. Can I change it so that the browser opens the link in a new tab or window? Alexandra Dunn To complete Steve's answer, if you

Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

pandoc: open link in new tab

singles According to this answer , it should be possible to use the following syntax [link](url){:target="_blank"} When used, links can be opened in a new tab pandoc. However, this option does not seem to be supported as it is not recognized when converting t

Button to open link in new tab

Alex Hawking I'm building a website and I want to open a new tab at a specific address when a button is clicked. Here is the html of my button: <button id="AsDownload" onclick="AsDownload();">Download</button> Here is my javascript: function AsDownload(){

phpBB open link in new tab

Adam I have a phpBB board 3 with prosilver style. When the user clicks the link in the post, the browser will open it in the same tab. Can I change it so that the browser opens the link in a new tab or window? Alexandre Tranchant To complete Steve's answer, if

Rails: Open link in new tab (with "link_to")

Dantes I have this code: <%= link_to image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook", :target => "_blank"), "http://www.facebook.com/mypage" %> When the user clicks the link, how can I open it in a new tab?

Rails: Open link in new tab (with "link_to")

Dantes I have this code: <%= link_to image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook", :target => "_blank"), "http://www.facebook.com/mypage" %> When the user clicks the link, how can I open it in a new tab?

Rails: Open link in new tab (with "link_to")

Dantes I have this code: <%= link_to image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook", :target => "_blank"), "http://www.facebook.com/mypage" %> When the user clicks the link, how can I open it in a new tab?

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Open link in AngularJS $state new tab

Alex Arvanitidis : I'm trying to implement the "open link in new tab" functionality using the $state.go function. It would be great if there was something like this: $state.go('routeHere', { parameter1 : "parameter" }, { reload : true, newtab :

React-Router open link in new tab

Mike: Is there a way to make React Router open links in new tabs? I tried it and it didn't work. <Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link> It can be blurred by adding something similar to what I have above to the onClick="f

notify onclick - open link in new tab and focus

Anoop Mayampilly Muraleedharan function spawnNotification(theBody, theIcon, theTitle, theLink) { var options = { body: theBody, icon: theIcon } var notification = new Notification(theTitle, options); notification.onclick = function() { var

Open a new tab when the link button is clicked

username I am using the code below to generate a button with a link. But the problem is that it opens in the same tab. I want it to open in a new tab. Can someone suggest me a method that works on most browsers. <form> <input TYPE="button" VALUE="Home Page"

Open a new tab (window) by clicking a link in jquery

k I have a web application that displays rows with goand deletebuttons . If the user clicks go, it should open a new tab/window with a url built from the row data. How to do this in jquery? What I am doing is: $('.go').click( function () { var wid = {{ wid|

React-Router open link in new tab

Mike Is there a way to make React Router open links in new tabs? I tried it and it didn't work. <Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link> It can be blurred by adding something similar to what I have above to the onClick="fo

React-Router open link in new tab

Mike: Is there a way to make React Router open links in new tabs? I tried it and it didn't work. <Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link> It can be blurred by adding something similar to what I have above to the onClick="f

Open a specific link in a new tab on page load

Suman Arman Previously, I got a solution from here on how to automatically open another link on page load. I got this code to do this window.setTimeout(&quot;autoClick()&quot;, 2000); // 2 seconds delay function autoClick() { var linkPage = document.getElemen

The link doesn't open in a new tab

Alan Weber Here is my javascript code, I want the link should open in new tab but it doesn't happen, how can I do that? <link href="http://vjs.zencdn.net/5.4.6/video-js.min.css" rel="stylesheet"> <script src="http://vjs.zencdn.net/5.4.6/video.min.js"></script>

JS Jquery - open link in new tab

Jaime Matos I have this JS in my website to display a custom text message and the message has a hyperlink. However, it opens it in the current window and I want to make sure it opens in a new tab. In this case, I can't figure out how to achieve this. var conce