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.
Related
I need to scrape a name and phone number that appears when clicking a button in a popup.
I was able to complete these steps:
Navigate through the site
Click on "Ver teléfono" option. This opens a popup
Fill the form
Click on "Enviar" option. This shows a loading gif and nothing else happens
Here is the used code: https://www.mycompiler.io/view/6ukTEftFwyh
The expected result (tested outside selenium) is that the popup shows the name and phone number after clicking the "Enviar" option
I have a code in python that uses selenium chromedriver to reach a webpage and collect some data but I want to solve a issue related with a pop-up window. For example, if you go to this webpage https://finance.yahoo.com/quote/BAC?p=BAC you will get two pop-up windows. One is for the acceptance of the collection of personal data (image below) and this I can handle well with the following code:
...
# Go to the website:
driver.get(main_url)
# Click accept button
driver.find_element(By.NAME, "agree").click()
...
The second one (image below) however I'm not being able to dismiss. I want the code to click on the "Maybe later" button but I cant find the button ID. Can someone help me?
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)
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.
Let's say there is an element with xpath "//a[#href]" that doesn't have a direct link to it and when clicked opens the UNKNOWN url in the current tab. Is there a way to make it open the URL in another tab instead of current one, so basically to "divert" the click into an another tab.
Here is some clarification:
driver.execute_script("arguments[0].click();", element) # clicks on an element
driver.execute_script("window.open()")
First line will click an element, and second would open a new tab, but as it is these lines aren't connected. Is there a way to execute just one script that would perform the click AND redirect it to be opened in a new tab?
I am not sure why you need that exactly but you could execute JS within a page that would add (or change) target attribute of your a tag to be target='_blank'.
Since you have added one, the link will open the target resource in a new tab (for Chrome).