I was trying to open another tab using undetected chrome driver in python. I found that when I use the javascript way chrome blocks my pop-up. I can't figure out what I'm doing wrong. Here's my code.
options = webdriver.ChromeOptions()
options.add_argument('--disable-popup-blocking')
if __name__ == '__main__':
chrome = uc.Chrome(options=options)
time.sleep(1)
chrome.get("https://www.google.com")
chrome.execute_script("window.open('https://google.com','_blank')")
You need to do some action or use sleep after opening the new tab, otherwise the browser will close immediately after opening a new tab. Try adding time.sleep(30) to the end of your code.
Related
I am trying to open/close tabs with chrome as my webdriver.
driver=webdriver.Chrome()
driver.get("http://www.google.com")
driver.find_element_by_xpath('//*[#id="zV9nZe"]/div') #accepting cookies
driver.execute_script("window.open('https://google.com','_blank')") #opening a new tab
driver.switch_to.window(driver.window_handles[0]) #switching back to previous tab
driver.close() #closing previous tab
driver.switch_to(driver.current_window_handle) #switching to current tab
driver.execute_script("window.open('https://google.com','_blank')") #opening a new tab
However when I try to open a new tab after closing the previous one I get the following error NoSuchWindowException: Message: no such window: target window already closed
I tried closing or opening tabs using the following code as a workaround
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
However both do nothing.
So I think I managed to fix this issue. First of all it appears that with Chromedriver sendkeys method won't work correctly with shortcuts -see Selenium with Python: send_keys() doesn't work on headless ubuntu .
Secondly by switching to the new window using
driver.switch_to.window(driver.window_handles[-1])
Instead of
driver.switch_to(driver.current_window_handle)
seems to fix this issue.
Thus the complete solution would be
driver=webdriver.Chrome()
driver.get("http://www.google.com")
driver.find_element_by_xpath('//*[#id="zV9nZe"]/div') #accepting cookies
driver.execute_script("window.open('https://google.com','_blank')") #opening a new tab
driver.switch_to.window(driver.window_handles[0]) #switching back to previous tab
driver.close() #closing previous tab
driver.switch_to.window(driver.window_handles[-1]) #switching to current tab
driver.execute_script("window.open('https://google.com','_blank')") #opening a new tab
Hope I helped some of you struggling with Selenium and tabs handling.
I would like to open multiple chrome windows. Once they open, however, they close at the end of the for loop. can anyone help me? thank you so much
for i in range(numeroTask):
i = webdriver.Chrome(PATH)
i.get("https://www.youtube.com/")
This is how you can do it. I'm using window.open() to open a new tab and then driver.switch_to.window to switch to it, so you can open a url.
from selenium import webdriver
driver = webdriver.Chrome()
windows_count = 3
for i in range(windows_count):
# Opens a new tab
driver.execute_script("window.open()")
# Switch to the newly opened tab
driver.switch_to.window(driver.window_handles[i])
# Navigate to new URL in new window
driver.get("https://youtube.com")
# Close all tabs:
driver.quit()
Hopefully this helps, good luck!
Updated, way to do it with multiple chrome windows:
from selenium import webdriver
driver = webdriver.Chrome()
windows_count = 3
for i in range(windows_count):
# Opens a new tab
driver.execute_script('window.open("https://youtube.com", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=800, height=900, top=10, left=10");')
# Close all windows:
driver.quit()
DO you want to open simultaneously? Then you should try threads, async functions.
In the following two blocks of code, the last line seems to execute differently and I'm not sure why:
from selenium import webdriver
import time
profile = webdriver.FirefoxProfile('Path to firefox profile')
profile.set_preference("dom.webdriver.enabled", False)
profile.update_preferences()
driver = webdriver.Firefox(profile)
driver.get("https://google.com/")
time.sleep(6)
driver.execute_script("window.open('');")
driver = webdriver.Firefox()
driver.get("https://google.com/")
time.sleep(6)
driver.execute_script("window.open('');")
In the first snippet, driver.execute_script opens up a new window, but in the second snippet,driver.execute_script opens up a new tab. Why do the two snippets have different behavior for driver.execute_script ?
My guess is that in the first snippet there is some profile preference that is causing it to create new windows instead of tabs but I'm not sure what profile setting to change to make the behavior match exactly.
Your second instance of browser is being opened because of driver = webdriver.Firefox(). driver.execute_script("window.open('');") will open new tab only.
Recently Selenium chrome webdriver started to run into problems by triggering Microsoft Malicious Software Removal Tool to request if it may reset browser settings. How to get around this? Is there an argument to add to options like --disable-extensions solved a popup problem before?
from selenium import webdriver
options = webdriver.chrome.options.Options()
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options)
A temporary solution may be
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
but nothing happens.
What works (but obviously not ideal) is to execute javascript to close the undesired tab:
time.sleep(0.2) # give some time for the tabs to appear
# just to understand that the first tab is counter-intuitivly the last of window.handles change the content of each tap
js = "document.getElementsByTagName('body')[0].innerHTML = 'This is handle {0}: {1}';"
for i, handle in enumerate(driver.window_handles):
driver.switch_to_window(handle)
driver.execute_script(js.format(i,handle))
# now close the msrt tab and make the desired tab active
handle_desired, handle_msrt = driver.window_handles # last handle is first tab
driver.switch_to_window(handle_msrt)
driver.execute_script('window.close()') # close the msrt tab
driver.switch_to_window(handle_desired)
I want to open quite a few URLs in different tabs using Selenium WebDriver & Python.
I am not sure what is going wrong:
driver = webdriver.Chrome()
driver.get(url1)
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
url2 = 'https://www.google.com'
driver.get(item2)
I looked up tutorials and it seems to me as though this code should do what I want. What actually happens is the browser opens, url1 opens as it should, a new tab opens as it should but url2 then loads in the original tab instead of the new one (even though the new tab appears to be the active one).
(I am using Chrome because when using Firefox I can't get it to load any URLs at all. Firefox opens but does not get the url requested. I have tried to find a solution to this but to no avail.)
Is there anything I can change in my code to get the new URL to open in the new tab?
Thanks for your help!
Here is a simple way, platform independent:
Code:
driver.execute_script("window.open('http://google.com', 'new_window')")
Switching back to the original tab:
Code:
driver.switch_to_window(driver.window_handles[0])
Checking the current title to be sure you are on the right page:
Code:
driver.title
For everything else, have fun!
There is a bug in ChromeDriver that prevents ctrl/command+T from working:
I canĀ“t open new tab in ChromeDriver
What you can do, as a workaround, is to open a link in a new tab and then switch to a new window using the switch_to.window(). Working sample:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.google.com")
# open a link in a new window
actions = ActionChains(driver)
about = driver.find_element_by_link_text('About')
actions.key_down(Keys.CONTROL).click(about).key_up(Keys.CONTROL).perform()
driver.switch_to.window(driver.window_handles[-1])
driver.get("https://stackoverflow.com")
Now the last driver.get() would be performed in a newly opened tab.
An alternative way to open a new window is to use JavaScript and the window handler to switch between them.
driver = webdriver.Chrome()
# Open a new window
# This does not change focus to the new window for the driver.
driver.execute_script("window.open('');")
# Switch to the new window
driver.switch_to.window(driver.window_handles[1])
driver.get("http://stackoverflow.com")
# close the active tab
driver.close()
# Switch back to the first tab
driver.switch_to.window(driver.window_handles[0])
driver.get("http://google.se")
# Close the only tab, will also close the browser.
driver.close()
If you look at your browser while you're executing it will look like the new window has focus, but to the webdriver, it doesn't. Don't be fooled by the visual. Also remember to select a new window handler when you close a tab as it will set the driver.current_window_handle to
selenium.common.exceptions.NoSuchWindowException:
Message: no such window: target window already closed from unknown error: web view not found
(Session info: chrome=<Your version of chrome>)
(Driver info: chromedriver=<Your chrome driver version> (<string of numbers>),platform=<Your OS>)
on .close() and it will throw that error if you try to do stuff with the driver at that stage.
you need to maximize your chrome for this
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.google.com/")
e = driver.find_element_by_tag_name("body")
ActionChains(driver).key_down(Keys.CONTROL).click(e).send_keys("k").key_up(Keys.CONTROL).perform()
here key_down(Keys.CONTROL) will hold down ctrl key, to get focus on page i am clicking body of the page, then click k