How would I make webdriver selenium open another tab, and copy something from that another tab, close it and paste it in the first tab?
How would I do this?
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 30)
Here's an example that opens a page, opens a new tab, copies something, closes the tab, and pastes the text into an input field on the new tab:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://seleniumbase.io/demo_page")
driver.switch_to.new_window("tab")
driver.get("data:text/html,<h1>Page B</h1>")
text = driver.find_element("css selector", "h1").text
driver.close()
window_handle = driver.window_handles[0]
driver.switch_to.window(window_handle)
driver.find_element("css selector", "input").send_keys(text)
driver.quit()
Related
I have a list of links (url), I need that each link opens in a different tab, example:
youtube.com
google.com
facebook.com
The script will open youtube, then in another window, it will open google, but I need to close the youtube tab, then I need to open the facebook tab.
My code is:
if(posts!=len(data)-1):
driver.execute_script("window.open('');")
chwd = driver.window_handles
driver.switch_to.window(chwd[-1])
driver.close()
driver.switch_to.window(chwd[-2])
driver.quit()
I assume you have the only one opened window before driver.execute_script("window.open('');").
Try
if(posts!=len(data)-1):
old_window = driver.window_handles[0]
driver.execute_script("window.open('');")
driver.switch_to.window(old_window)
driver.close()
driver.switch_to.default_content()
driver.quit()
wait=WebDriverWait(driver,60)
urls=["https://www.youtube.com/","https://www.google.com/"]
chwd = driver.window_handles
for url in urls:
old = driver.current_window_handle
driver.execute_script("window.open('{arguments[0]');",url)
wait.until(EC.number_of_windows_to_be(len(chwd)+1))
driver.switch_to.window(old)
driver.close()
driver.switch_to.default_content()
driver.quit()
I would do it like this to close the window as soon it opens so you can don't accidentally close stuff. Then switch back to default.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
There is a link embedded in a web element in the Main Tab, I want to open that link in a new tab in the same window using Selenium Webdriver and python.
Perform some tasks in the new tab and then close that tab and return back to the main tab.
Manually we will do this by right-clicking on the link and then select "open in new tab" to open that link in new tab.
I am new to Selenium. I am using Selenium and BeautifulSoup to web scrape. I only know how to click on a link. I have read through many posts but couldn't find a proper answer
url = "https://www.website.com"
path = r'path_to_chrome_driver'
drive = webdriver.Chrome(path)
drive.implicitly_wait(30)
drive.get(url)
source = drie.page_source
py_button = driver.find_element_by_css_selector("div[data-res-position = '1']")
py_button.click()
I expect the link in div[data-res-position = '1'] to open in a new tab
As there is a link embedded within in the webelement in the Parent Tab, to open the link in a New Tab in the same window using Selenium and Python you can use the following solution:
To demonstrate the workflow the url https://www.google.com/ was opened in the Parent Tab and then open in new tab functionalty is implemented through ActionChains methods key_down(), click() and key_up() methods.
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.google.com/")
link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
Note: You need to replace (By.LINK_TEXT, "Gmail") with your desired locator e.g. ("div[data-res-position = '1']")
Browser Snapshot:
You can find a relevant Java based solution in Opening a new tab using Ctrl + click combination in Selenium Webdriver
Update
To shift Selenium's focus to the newly opened tab you can find a detailed discussion in Open web in new tab Selenium + Python
This is trick can be achieve if your locator return a correct href.
Try this first:
href = driver.find_element_by_css_selector("div[data-res-position = '1']").get_attribute("href")
print(href)
If you get correct href, you can do:
#handle current tab
first_tab = driver.window_handles[0]
#open new tab with specific url
driver.execute_script("window.open('" +href +"');")
#hadle new tab
second_tab = driver.window_handles[1]
#switch to second tab
driver.switch_to.window(second_tab)
#switch to first tab
driver.switch_to.window(first_tab)
Hope this helps.
Following import:
selenium.webdriver.common.keys import Keys
You have to send key:
py_button.send_keys(Keys.CONTROL + 't')
I have tried all the methods in similar questions and only one of them worked which was to use javascript.
driver.execute_script("window.open('')")
#this works
ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
#this doesn't
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
#this doesn't work either
I'd like to get the second way to work, since it seems the most readable and sensible, am I doing something wrong in my code? Or is there any option I need to change in Selenium to enable opening tabs like this?
Edit: The 2nd and 3rd method don't produce any result at all. Not even an exception.
Below code works for me for opening and closing tabs:
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
time.sleep(2)
# open new tab
driver.execute_script("window.open('');")
# switch to the new tab and open new URL there
driver.switch_to.window(driver.window_handles[1])
driver.get("http://www.python.org")
time.sleep(2)
chld = driver.window_handles[1]
driver.switch_to.window(chld)
driver.close()
The second way didn't work for me as well.
import the selenium module
from selenium import webdriver
creates selenium object
driver = webdriver.Chrome()
gets the url needed for the driver object
url = "https://www.google.com/"
Open a new window
driver.execute_script("window.open('');")
Close the tab
driver.close()
There is a link embedded in a web element in the Main Tab, I want to open that link in a new tab in the same window using Selenium Webdriver and python.
Perform some tasks in the new tab and then close that tab and return back to the main tab.
Manually we will do this by right-clicking on the link and then select "open in new tab" to open that link in new tab.
I am new to Selenium. I am using Selenium and BeautifulSoup to web scrape. I only know how to click on a link. I have read through many posts but couldn't find a proper answer
url = "https://www.website.com"
path = r'path_to_chrome_driver'
drive = webdriver.Chrome(path)
drive.implicitly_wait(30)
drive.get(url)
source = drie.page_source
py_button = driver.find_element_by_css_selector("div[data-res-position = '1']")
py_button.click()
I expect the link in div[data-res-position = '1'] to open in a new tab
As there is a link embedded within in the webelement in the Parent Tab, to open the link in a New Tab in the same window using Selenium and Python you can use the following solution:
To demonstrate the workflow the url https://www.google.com/ was opened in the Parent Tab and then open in new tab functionalty is implemented through ActionChains methods key_down(), click() and key_up() methods.
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.google.com/")
link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
Note: You need to replace (By.LINK_TEXT, "Gmail") with your desired locator e.g. ("div[data-res-position = '1']")
Browser Snapshot:
You can find a relevant Java based solution in Opening a new tab using Ctrl + click combination in Selenium Webdriver
Update
To shift Selenium's focus to the newly opened tab you can find a detailed discussion in Open web in new tab Selenium + Python
This is trick can be achieve if your locator return a correct href.
Try this first:
href = driver.find_element_by_css_selector("div[data-res-position = '1']").get_attribute("href")
print(href)
If you get correct href, you can do:
#handle current tab
first_tab = driver.window_handles[0]
#open new tab with specific url
driver.execute_script("window.open('" +href +"');")
#hadle new tab
second_tab = driver.window_handles[1]
#switch to second tab
driver.switch_to.window(second_tab)
#switch to first tab
driver.switch_to.window(first_tab)
Hope this helps.
Following import:
selenium.webdriver.common.keys import Keys
You have to send key:
py_button.send_keys(Keys.CONTROL + 't')
I would like to do automated script. I define an array in start of the program.
Later program is opening browser and search in google some specific word (for example apple), next program is clicking in first of string from array and close browser. Later is doing the same but it will click in second of word from array.
My code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:/Users/Daniel/Desktop/chromedriver.exe")
driver.implicitly_wait(30)
driver.maximize_window()
hasla = ["ispot","myapple"]
for slogan in hasla:
driver.get("http://www.google.com")
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()
search_field.send_keys("apple")
search_field.submit()
name = driver.find_element_by_link_text(slogan)
name.click()
driver.quit()
driver.implicitly_wait(10)
When Im starting this program from console in windows.
Program is opening browser, looking for apple click in ispot and clsoe browser but its not opening new browser and it not doing the same for next string in array. Any solution ?
In console i have this:
screen
You're exiting the browser in your for loop, so the second iteration can't do anything because there's not a browser open. You could try opening a new tab and closing the old one if you need to start fresh each time. Try this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:/Users/Daniel/Desktop/chromedriver.exe")
driver.implicitly_wait(30)
driver.maximize_window()
hasla = ["ispot","myapple"]
for slogan in hasla:
driver.get("http://www.google.com")
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()
search_field.send_keys("apple")
search_field.submit()
name = driver.find_element_by_link_text(slogan)
name.click()
# Save the current tab id
old_handle = driver.current_window_handle
# Execute JavaScript to open a new tab and save its id
driver.execute_script("window.open('');")
new_handle = driver.window_handles[-1]
# Switch to the old tab and close it
driver.switch_to.window(old_handle)
driver.close()
# Switch focus to the new tab
driver.switch_to.window(new_handle)
If you are closing the tab you won't be able to see the results. You might want to keep it open and just go to the new tab. In which case, just remove driver.close().
Alternatively, if you really want to completely close out the browser each time and reopen it, you just need to include the first three lines in your for-loop.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
hasla = ["ispot","myapple"]
for slogan in hasla:
driver = webdriver.Chrome("C:/Users/Daniel/Desktop/chromedriver.exe")
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("http://www.google.com")
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()
search_field.send_keys("apple")
search_field.submit()
name = driver.find_element_by_link_text(slogan)
name.click()
driver.quit()
To answer your second question:
First, import NoSuchElementException:
from selenium.common.exceptions import NoSuchElementException
Then replace your try/except with this:
try:
name = driver.find_element_by_link_text(slogan)
name.click()
except NoSuchElementException:
print('No such element')
driver.quit()
It's still going to close the browser and go to the next iteration if the element is found or not.