I'm trying to open the broswer and search some urls with the module webbroswer of python,
This is my code
import webbrowser
b = webbrowser.get('firefox')
b.open('google.com')
b.open('stackoverflow.com', new=0)
This code works but it opens the urls in two different tabs, i want that it searchs before for google.com and after in the same tab it has to search for stackoverflow.com. I read in the docs that for open a new thab the new parameter has to be set equal to 2 but it is 0 now, why it keeps opening new tabs?
import webbrowser
webbrowser.open('google.com', new = 0)
And the docs says:
If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible
The same question has been answered here.
try this:
webbrowser.open_new(url)
Open url in a new window of the default browser, if possible, otherwise, open url in the only browser window.
webbrowser.open_new_tab(url)
Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to open_new().
b.open('stackoverflow.com', new=0)
Your new should be set to 2 not 0.
Related
I want to create a script that open links automatically.
But i want the program to open the link in a one page pop up..
i tried to open two links one by one in the same tab, but it always open it in a new Tab in the same Browser.
this is the code:
import webbrowser
import time
search = input("Enter URL Here : ")
search1 = input("Enter Other URL Here : ")
min = int(input("Enter Wait Time : "))
webbrowser.open(search)
time.sleep(min)
webbrowser.open(search1, new=0)
How can I get the URL to be opened in the same Tab as it should be using new=0?
After reading some documentation, the Webbrowser module lacks what you asked for.
BUT, seems like Selenium would work for you. I did some research and I ended up in this Reddit post. You would need to install the module plus the driver of the browser you want to use. If you wanna try, this code snippet should work.
from selenium import webdriver
link1="https://www.example.com"
link2="https://www.google.com/"
driver=webdriver.Chrome()
driver.get(link1)
driver.get(link2)
Whether this is opening a tab, saving a bookmark, printing a file or whatever, Selenium can't register key presses. I've tried the following approaches:
1. driver.find_element_by_tag_name("body").send_keys([insert key here])
2. ActionChains(driver).send_keys([insert key here]).perform()
3. ActionChains(driver).key_down(Keys.CONTROL).send_keys([insert key here]).key_up(Keys.CONTROL).perform()
I've also tried putting driver.find_element_by_tag_name('body').click() in front of the each of those lines to force the browser to focus on the page, but even this doesn't work.
THank you in advance for your help.
driver = webdriver.Firefox()
driver.get("https://www.google.com")
input= driver.find_element_by_xpath('//input[#title="Search"]')
input.send_keys("hi this is a test")
input.send_keys(Keys.CONTROL+"a")
input.send_keys(Keys.CONTROL+"x")
time.sleep(5)
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()
time.sleep(5)
This code types something on the google search field , selects all, cuts the text and again paste it back .
It works fine , please add the code you tried and the website where you are trying it
I have a selenium webautomation program for tradingview. It all stays in one tab but when I open a new tab/link which it than switches to and which I than want to close after that, it closes the original tab. It looks like this:
for i in range(1, 1001):
input_ = '/html/body/div[8]/div/div[4]/table/tbody/tr[x]/td[1]/div/div[2]/a'
list_input_ = list(input_)
list_input_[44] = str(i)
input_ = ''.join(list_input_)
stock = driver.find_element_by_xpath(input_)
stock.click()
time.sleep(5)
driver.close()
print(input_)
if i > 9:
break
The stock = driver.find_element_by_xpath(input_) and stock.click() opens the new tab/link to the left. But than driver.close() closes the tab I was previously in. I want it close the tab that it just opened and that I'm currently in. Which it supposed to do. It also seems like that none of the selenium commands work on this newly opened tab and only work on the original tab.
The copied element of the link it opens looks like this:<a class="tv-screener__symbol apply-common-tooltip" href="https://www.tradingview.com/symbols/NASDAQ-STAF/" target="_blank" rel="noopener">STAF</a>
Did I do something wrong here? Thanks in advance
It is because you haven't switched focus to the new tab that you have opened. To switch focus to the newly opened tab, use this line:
driver.switch_to.window(driver.window_handles[1])
This line switches the focus to the 2nd tab that is open your browser. Then, you can perform the operations that you want in this tab. Then if you want to close this tab, then just use:
driver.close()
This would close the 2nd tab (the tab that is currently in focus).
I need to get all the urls from all open Google Chrome tabs in python 3 without intefering with the user. Im on Windows 10 using Microsoft Visual Studio Python3
Ive tried:
Opening it directly with open(path to current tabs)-- doesnt work because i have no permission- i think its locked because chrome activly writes to it.
Current_Tabs_Source = open(r"C:\Users\Beni\AppData\Local\Google\Chrome\User
Data\Default\Current Tabs", "r")
Current_Tabs_Raw = Current_Tabs_Source.read()
print(Current_Tabs_Raw) #just for checking
PermissionError: [Errno 13] Permission denied
Opening through sglite3 -- doesnt work because its locked. And i cant find a password anywhere. Ive tried to open the History for the urls but it doesnt work anyways.
import sqlite3
from os import path
data_path = path.expanduser('~') + r"\AppData\Local\Google\Chrome\User
Data\Default"
files = listdir(data_path)
history_db = path.join(data_path, 'history')
c = sqlite3.connect(history_db)
cursor = c.cursor()
select_statement = "SELECT urls.url, urls.visit_count FROM urls, visits
WHERE urls.id = visits.url;"
cursor.execute(select_statement)
results = cursor.fetchall()
print(results) #just for checking
sqlite3.OperationalError: database is locked
Using selenium and a 3rd party chrome extension to copy all urls to the clipboard -- doesnt work because these extensions only work in the active selenium window. So the Windows with the tabs in it that i want dont get copied.
Ive considered hacking together a chrome extension that copys the urls every 30 sec to a temp file. But i only know minimal Javascript so this thing is driving me mad.
So does anyone know a way to do this in Python ? Any other solution is greatly appreciated.
If you want to access the database, you should close all browsers.
(Source)
When we're accessing the SQLite database of any browser, we have to close that particular browser first.
Moreover, the SQL command being used here will fetch all the duplicate rows.
Need to change `select_statement'
select_statement = "SELECT distinct urls.url, urls.visit_count FROM urls, visits WHERE urls.id = visits.url;"
Further, we need to add a loop to print all the 'urls' from history database of chrome.
for url, count in results:
print(url) # print urls line by line
However, this will give us the whole history of the Chrome browser but not the required URLs of all the presently opened tabs.
You can use windows shadow copy service to make copy of locked sqlite database. And then read it normally.
There is python module for that https://github.com/sblosser/pyshadowcopy
I just have a simple question. When I do something like
import webbrowser
webbrowser.open('www.google.com')
it works,and opens a page using my default browser
However,when I store the url in a variable, it doesn't open it..doesn't throw up any errors or anything either.. something like:
import webbrowser
url=raw_input("Enter url")
webbrowser.open(url)
Anyone have any ideas as to why this might be happening?
The solution to this problem is to include http:// in the address, i.e., http://www.google.com.