Is there any way to determine which tab the user is viewing, like say the active tab (in selenium) is the last one and same for the user, then the user goes back to the first one, how do I know this with selenium?
From the information that has been gathered there seem to be no way to do this. What you can do is see if the current window is the active one using:
driver.execute_script("return document.visibilityState") === "visible"
But you cannot loop through the tabs and check this property since as soon as you change tab the tab you change to will also become the visible one.
While switching tabs you need to change Selenium's focus from one tab to other.
You can find a relevant detailed discussions in:
Open web in new tab Selenium + Python
How to switch window handles using Selenium and PythonHow to switch into Window using Index in
Selenium
Selenium may not have the focus on the tab you are currently visualizing. It's the user's discretion on which tab you want Selenium to focus.
You can always extract the current window handle on which Selenium have the focus using the current_window_handle attribute as follows:
print(driver.current_window_handle)
Related
I am trying to get the index or window id of currently visiting page not the active tab.
I am trying to access a specific tab in multiple tabs opened by bot I want to know that if user clicked on the unfocused tab e.g. tab no 3 but the active/focus tab is tab no 6. I want to shift the focus to user visiting page.
Sample code of mine which is not working.
for i in web.window_handles:
if not web.execute_script("return document.visibilityState") == "visible":
web.switch_to.window(i)
current_opened_window = web.current_window_handle
else:
current_opened_window = i
Sample Picture
Presumably Selenium can focus only on a specific tab at a time and to shift the focus to other tabs you need tab handling.
Hence, any click attempt on a WebElement will try to locate the element on the focused tab/page first. Incase the element isn't found NoSuchElementException would be raised.
I have a python selenium code to test a website. Now once the driver click on a link in the 1st tab, it opens it up in a new tab, but the focus stays on the previous tab, and I am not able to reference any elements in the new tab.
How can change the focus to the new?
Also both the tabs are open in the same browser and I am using Chrome browser
name each window if you are working with a few number of windows. using this chunk you can name and switch between tabs
window1= driver.window_handles[1]
driver.switch_to_window(window1)
You can loop through the window handles, record the counts of window handles and switch with count - 1. Check below.
n = 0
for handle in driver.window_handles:
n+=1
driver.switch_to_window(n-1)
If you encounter any difficulty, please paste your code for further elucidation.
I am working on a web application in the flask. My main page displays the list of author names. When I click on the name, a new tab opens to display the author's profile information. If I click on another name from the main page, another tab gets opened.
I want a solution to open only one tab if I click on the author's name one by one e.g previously opened tab should get closed before opening new tab.
Is it possible?
thanks in advance
The web browser itself is responsible for opening / disposing of tabs when you click on links, and the idea of a "tab" doesn't necessarily even need to exist, some browsers don't have such a thing. For example, the mobile browser Firefox Focus can only have one page open at a time.
previously opened tab should get closed before opening new tab. Is it possible?
To put it simply, no. There is such a thing as javascript's Window.close(), but this can only apply to the page that the code was executed in, you have no control over other tabs that a user may or may not have open on their local machine. If you consider the example of Google Chrome, the basis of the "tab" construct is that tabs are isolated objects, in fact they each get their own OS process and don't directly communicate.
So what can you do to address your problem? Here are a few ideas
When an author is clicked in the list, use javascript to insert a new element containing their profile information without loading a new page. You could add the javascript tag to your queston to try and get some help in that area
When an author is clicked in the list, use flask's templating to reload the same page but with the selected author's profile template embedded in the page. https://jinja.palletsprojects.com/en/2.10.x/templates/#include
In either case, this question seems to be escaping the domain of Python, consider re-tagging or opening a new question with more focus
I'm writing some automated GUI testing with selenium (Python binding + Firefox driver). On this page we're having problem with, there is button that I want to click but it's at the lower part of the page (I'm selecting the button via id). The default size of the Firefox window isn't large enough to show it. So the actual clicked element is one from the tab bar which is always visible.
If I manually resize the window during the test, it runs smoothly.
This looks like a bug to me TBH. I'm wondering if this is a known feature and a work around exists.
You can use Actions Chains to scroll to the element
actions = ActionChains(driver)
actions.move_to_element(element).perform()
That will make the button visible and you will be able to click on it. You can also use explicit wait to make sure the button is visible.
You can call location_once_scrolled_into_view on the element. It is a property that returns the elements location, but it has the added side-effect of scrolling to the element first if it is not in view already.
element.location_once_scrolled_into_view.
I am using the Selenium Python Bindings for web browser automation in Chrome. As part of the automation script, I click on a link and the website opens the page in a new tab. However, the WebDriver object in my python script is still pointing to the first tab.
I have tried all of the options offered on this answer but none were successful.
The only code I have been able to get to work so far is this:
driver.switch_to.window(driver.window_handles[1])
The problem I have with this is that I'm afraid I can't guarantee that the new tab will be at index 1, nor do I think I can guarantee that the new tab is at the last index. I did try using keys like this:
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
But using some logging I saw that the driver was still pointing to the first tab. Is there a way to focus on the current tab that chrome is showing?
Whenever I have cases like these I switch to the last window handle, so far it worked for me:
driver.switch_to.window(driver.window_handles[-1])