Open multiple URLs in same browser with selenium python
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome(executable_path=r"C:\Users\prave\Downloads\travelBA\chromedriver.exe")
driver.maximize_window()
driver.delete_all_cookies()
urls = ["https://business.google.com/u/0/edit/l/10199720925622488243?hl=fr",
"https://business.google.com/u/0/edit/l/13532588171385373346?hl=fr",
"https://business.google.com/edit/l/18307083220547614220",
"https://business.google.com/u/0/edit/l/08603059593698723407?hl=fr",
"https://business.google.com/edit/l/00810825496818981035"]
for posts in range(len(urls)):
print(posts)
driver.get(urls[posts])
if(posts!=len(urls)-1):
driver.execute_script("window.open('');")
chwd = driver.window_handles
driver.switch_to.window(chwd[-1])
// you can move to specific handle
chwd = driver.window_handles
print(chwd)
find the window handle and switch to it
how to open different urls at the same time by using python selenium?
You can try below to open 50 URLs one by one in new tab:
urls = ["http://first.com", "http://second.com", ...]
for url in urls:
driver.execute_script('window.open("%s")' % url)
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 multiple browser tab that has the same url using python +selenium
I am not sure why are you using Selenium for load testing, You may wanna have a look at jmeter
Having said that, for this particular task, you can switch between tabs. You can go to any tab from any tab in Selenium.
I would assume that you have click somewhere on the first tab and then a new tab must have been opened, right ?
If that is the case, you can use the below sample code :
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("Your URL here")
wait = WebDriverWait(driver, 10)
first_tab_handle = driver.current_window_handle #Storing the current or first tab windows handle
driver.find_element_by_id('some id').click() # the moment this click take place, you would see a new tab with some other or with same content (Assuming this click triggers an event)
two_tabs_handles = driver.window_handles # Note that, two_tabs_handles will have previous tab (first tab) as well as second tab handles.
driver.switch_to.window(two_tabs_handles[1]) # Note that, window_handles returns a list in python, so [0] denotes the first tab whereas [1] denotes the first tab.
#perfrom some operation here on tab 2 (e.g click somewhere to open tab3 using selenium)
three_tabs_handles = driver.window_handles
driver.switch_to.window(two_tabs_handles[2])
and so on...
PS :- Read out the comments for better understanding.
Opening several URLs in same tab one by one using Selenium Python
Its because you are closing the browser every time loop ends , you just need to keep driver.close()
outside the loop.
import json
from selenium.webdriver import Chrome
with open('path to json file', encoding='utf-8') as s:
data = json.loads(s.read())
for site in data['sites']:
driver = Chrome('path to chrome driver')
driver.get(data['sites'][site])
driver.get_screenshot_as_file(site + '.png')
driver.close()
how to load multiple urls in driver.get()?
Try below code :
def getUrls(targeturl):
driver = webdriver.Chrome(executable_path=r" path for chromedriver.exe")
driver.get("http://www."+targeturl+".com")
# perform your taks here
driver.quit()
for i in range(3):
webPage = ['google','facebook','gmail']
for i in webPage:
print i;
getUrls(i)
How do I open one URL at a time from text file with Selenium?
First you read the URLs into a list and convert that list to an iterator. That allows to simply use next
to get the next URL. So pressing the button will simply open the next
URL and if there are no more to open it will stop the function from further execution:
import tkinter as tk
from selenium import webdriver
PATH ="C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
with open('myfile.txt') as file:
urls = iter([line.strip() for line in file])
def open_next():
try:
driver.get(next(urls))
except StopIteration:
print('no more urls')
return
root = tk.Tk()
btn = tk.Button(root, text='Open next url', command=open_next)
btn.pack(padx=10, pady=10)
root.mainloop()
A couple of other things:
You don't need to use .readlines
and it is better to use with
(context manager) to open files. There is no need to use lambda
in command
if the function takes no arguments. Also don't use time
, it has no place in this architecture since it freezes the entire thread and process and you rarely if ever want that to happen to a GUI. Also Variable
s such as StringVar
s are not really needed for Button
s, just use their text
argument and if you need that to change use config
.
Also:
I strongly advise against using wildcard (*
) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2
and so on or import the whole module: import module
then You can also use an alias: import module as md
or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.
Unable to scrape multiple URLs from a website using selenium python
Working solution,
n_links = [ele.find_element_by_tag_name('a').get_attribute('href') for ele in news_links]
Related Topics
Using a Global Variable With a Thread
Wait Until a Certain Process (Knowing the "Pid") End
How to Check Whether All Elements of Array Are in Between Two Values
How to Read Pdf Files One by One from a Folder in Python
Hiding Axis Text in Matplotlib Plots
How to Restart Airflow Webserver
Regex to Match Digits and At Most One Space Between Them
Setting Matplotlib Colorbar Range
How to Show a Pandas Dataframe into a Existing Flask HTML Table
Python: Requests.Exceptions.Connectionerror. Max Retries Exceeded With Url
How to Repeat Each Test Multiple Times in a Py.Test Run
How to Get All Days in Current Month
How to Copy/Repeat an Array N Times into a New Array
Unable to Install Psycopg2 (Pip Install Psycopg2)