How to get URL of currently open web browser tab - python

Hi I want to be able to grab the url of the currently open tab in a web browser. I can do this with Selenium but it requires that the web browser is opened with Selenium's webdriver. I instead want to be able to run this script with a Firefox or Chrome window already open, and have it return the url of the currently open tab.

Related

How can one get the url of a new opened tab by the first website that was running in Selenium Python

I am trying to get the URL of the newly opened tab so I can work on it and find elements in that tab. How can one get a URL of a new opened tab that was opened by the website on click of a button using selenium in python.
In general Selenium automation, we first switch to newly opened tab and then we can get the URL and we can then interact with the element.
for switching to new tab, you can use the below code:
original_win_handle = driver.current_window_handle
# code to click on the first page and now expecting a new tab to be opened
all_win_handles = driver.window_handles
driver.switch_to.window(all_win_handles[1])
and now you've switched it successfully, you can get the URL by running the below code:
print(driver.current_url)

Selenium Python can't open a second page while using chrome webbrowser

I am using Selenium to open different pages of a site. Have tried multiple times but the browser does not open a second webpage after the initial GET call. Have tried on Chrome. Below is the screenshot of the error:
enter image description here
Looking forward to your support on it.

Selenium page loads as blank unless browser is manually opened with same profile

I am using selenium for a crawling project, but I struggle with a specific webpage (both chrome and firefox).
I found 2 workarounds that work to an extend but I want to know why this issue happens and how to avoid it.
1) Opening chrome manually and then opening selenium with my user profile.
If i manually start chrome and then run:
from selenium import webdriver
options.add_argument(r"user-data-dir=C:\Users\User\AppData\Local\Google\Chrome\User Data")
driver = webdriver.Chrome(options=options)
the page loads as intended
2) Passing a variable in the request
by appending /?anything to the url the page loads as intended in selenium
For some reason the webpage has a function in the header despite not loading... I suspect this could be a clue but I do not know enough to determine the cause.

How to open new tab in Chrome with Selenium-chromeDriver in Python

Is there anybody who used ChromeDriver to open new tabs in Chrome?
With this code I can open my index.html in Chrome:
driver = webdriver.Chrome("/usr/bin/chromedriver")
driver.get("localhost:3000")
And then I want to open localhost:3000/cloudPeer.html in a new tab:
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + 't')
driver.get("localhost:3000/cloudPeer.html")
This works well in Firefox, but in Chrome, it can't open a new tab, it just refresh the first page, and open the new page, the first page is now lost.
I want to open these two pages at the same time and do some communication tests between them.
This works for chrome-
driver.execute_script("window.open('https://www.google.com');")

How to ensure that we have only one instance of a given page opened in the web browser using python

For example i want to open HTML files in the browser using python (exactly the webbrowser module ) but i want that page to be in the active tab and only one instance of that page should be opened in the whole web browser.
Here is simple python code that i'm using
import webbrowser
def openWebPage(url):
webbrowser.open(url)
## before opening the html page i want to make sure
## it is not opened already, otherwise select it and make it the active tab
openWebPage('/var/www/html/index.html')

Categories