How to change URL in Selenium without opening new tab (python)? - python

I saw some .navigate() functions to do that in java but it's not in python.
So, how can I just change the URL of currently opened window without opening a new tab?

Just call driver.get('yourURL') again. Example:
driver = webdriver.Chrome()
driver.get('https://google.com')
print(driver.current_url)
driver.get('https://gmail.com/')
print(driver.current_url)
Output:
https://www.google.com/
https://www.google.com/intl/id/gmail/about/#

Related

Selenium checking if tab is open

I'm using selenium to check if a url is open in a tab, and if it is, print found. Here's my code:
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
driver.get("http://www.google.com/")
tabs_list = []
for handle in driver.window_handles:
driver.switch_to.window(handle)
tabs_list.append(driver.current_url)
url = 'https://www.google.com/'
for tab in tabs_list:
if url in tab:
print('found')
When I run this, nothing happens for several seconds, then a new Firefox window is opened, with an orange search bar. found also is not printed, even though google.com is running. When I print my tabs_list to see what is inside, I get ['about:blank'].
On further testing I discovered that it was searching for tabs in the new window. Is there any way to make it search through an existing window, instead of creating a new one?
Here's the solution, you're using url in quotes in other quotes, like:
driver.get('"http://www.google.com/"')
so that one should work perfectly:
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get("https://www.google.com/")
driver.quit()

multiple chrome windows with webdriver python

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.

Selenium script executes differently based on whether or not default Firefox profile is used

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.

Python Selenium - URL won't open in browser instance

This particular URL won't open via python selenium script below. This same code works for most of the urls I have tried it on.
chrome_driver = 'chromedriver.exe' # Change this to your chrome driver path
driver = webdriver.Chrome(chrome_driver)
driver.get(url)
There is no any error message displayed, it will only show blank page and nothing will happen. When manually typed into regular chrome browser, it opens correctly but when manually into chrome browser instance, it won't work just like the script.
What is the solution to fix this?

Selenium won't open a new URL in a new tab (Python & Chrome)

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

Categories