What Is the Fastest Way to Open Urls in New Tabs via Selenium - Python

Open web in new tab Selenium + Python

Editor's note: This answer no longer works for new Selenium versions. Refer to this comment.


You can achieve the opening/closing of a tab by the combination of keys COMMAND + T or COMMAND + W (OSX). On other OSs you can use CONTROL + T / CONTROL + W.

In selenium you can emulate such behavior.
You will need to create one webdriver and as many tabs as the tests you need.

Here it is the code.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.google.com/")

#open tab
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
# You can use (Keys.CONTROL + 't') on other OSs

# Load a page
driver.get('http://stackoverflow.com/')
# Make the tests...

# close the tab
# (Keys.CONTROL + 'w') on other OSs.
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')

driver.close()

Is there a way to open multiple tabs at once with Selenium + Python?

You can use send_keys with (Ctrl and T) to open new tab and then use get to enter the url you wish. Also you can track switch back and forth to all the tabs you have opened using window handles (driver.window_handles and driver.switch_to.window(handles)).

How can I open a new tab with selenium python?

You can do it as

from selenium import webdriver

driver.get("https://www.youtube.com")
search = driver.find_element_by_id("search")

driver.execute_script("window.open('https://www.google.com')")

How to open a particular link in a new tab in selenium/python

Here is the solution.

Sourcecontrol = driver.find_element_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]')
Sourcecontrol.click();
Changerequest=driver.find_element_by_xpath( '//td[@class="confluenceTd"]/a[contains(.,"Change: ")]')
testvalue = Changerequest.get_attribute('href')
driver.execute_script("window.open(arguments[0])",testvalue)

How to loop through multiple url and open urls in new tabs using selenium python chromedriver

You should be able to open all the details links in new tabs using the below logic.

driver.get("https://xxxxxx/blogs/")
if driver.find_element_by_xpath("(//span[@class='ui-datepicker-month'][contains(.,'May')])[1]"):
main_window = driver.current_window_handle
# get the number of details to click
addr = len(driver.find_elements_by_xpath("//a[@class='details'][contains(.,'Details')]"))
# iterate through all the details links (used the index rather elements list as it may lead to staleeleemnt exception after clicking on the first detiails link)
for addrNum in range(addr):
# get the details element based on index
ele = driver.find_element_by_xpath("(//a[@class='details'][contains(.,'Details')])[" + str (addrNum+1) + "]")
# get the href of the link
href = ele.get_attribute('href')
# open the href in another tab
driver.execute_script("window.open('" + href +"');")
# switching to parent window (on safer side)
driver.switch_to.window(main_window)

How to open each url in new tab in chrome driver using python

You can try this code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from lxml import html

driver = webdriver.Chrome()
driver.get('https://www.google.com/')

file = open('f:\\listofurls.txt', 'r')

for aa in file:
#open tab
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
# You can use (Keys.CONTROL + 't') on other OSs

# Load a page
driver.get(aa)
# Make the tests...
soup = html.fromstring(driver.page_source)
name = soup.xpath('//div[@class="name"]//text()')
title = soup.xpath('//div[@class="title"]//text()')
print(name, title)
time.sleep(3)

driver.close()

I need to Open a new tab and Paste a URL to the Url line at the browser, using Python+Selenium. Doesn't work with Keys for some reason

Update 1 :

driver.get("https://www.google.com")
driver.execute_script("window.open('about:blank','_blank');")
all_handles = driver.window_handles
driver.switch_to.window(all_handles[1])

driver.get("https://www.amazon.com")

Update 2 :

You can use pyperclip to paste as well. see below

import pyperclip
action = ActionChains(driver)
action.send_keys(pyperclip.paste()).perform()


Related Topics



Leave a reply



Submit