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).
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)
How do I display a text that I copied with the function send_keys (Keys.CONTROL, "v") to display it with the print command?
Try to get an input and save it to a variable.
Like this:
your_text = driver.find_element_by_id("my_id").get_attribute("value")
print(your_text)
No, you can't do this with selenium alone. Selenium is a browser control and the clipboard is a system level process. You would need to involve other software to properly manipulate the clipboard.
See alos this answer for confirmation of the above statements.
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
This question already has answers here:
selenium.common.exceptions.NoSuchWindowException: Message: no such window error clicking an element within an iframe using Selenium and Python
(1 answer)
"NoSuchWindowException: no such window: window was already closed" while switching tabs using Selenium and WebDriver through Python3
(3 answers)
org.openqa.selenium.NoSuchWindowException: Unable to get browser while trying to open Internet Explorer 11 through Selenium and IEDriverServer
(2 answers)
Closed 2 years ago.
So I have the first tab; the main tab. I want to open multiple tabs using selenium and once their task is done, to close them until we're back to the original first tab
url = "https://www.google.com/"
i=4
for each in reversed(range(i, 0, -1)):
print(each)
driver.execute_script(f"window.open('{url}');")
driver.switch_to.window(driver.window_handles[each])
#do stuff
driver.close()
driver.switch_to.window(driver.window_handles[each-1])
I get the following error:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed from unknown error: web view not found
I know its something to do with the index values of each tab, where 0 would be the inital tab, correct me if I'm wrong, but I can't get it to work.# properly.
The best I could do is to get it to close each tab randomly until left with 2, then suddenly it closes both of them (no tabs left, exits browser) when reach the value of 1
your issue starts here "driver.close()" - close method closes the current window - no matter how many tabs you have, but it doesn't kill chrome instance (drive.quit() - does the job). If you want to close the current tab you can use a workaround like
# Close current tab for windows/linux
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
# Close current tab for Mac
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')
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.