How to select new opened window in Selenium? - python

I'm trying to solve a little problem.
There is a web page - there is a button on this web page. When I click on this button, a new window is opened.
I'm curious, how to tell the Selenium that I want to see a page source of this new window?

You need to switch to the other window. Since the webdriver can only deal with your existing windows , you need to look into the window handling. You can find an example regarding switching windows below:
WebDriver driver = new FirefoxDriver();
driver.get("https://hdfcbank.com");
Thread.sleep(6000L);
Actions mouse = new Actions(driver);
WebElement address = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[2]/div[2]/ul/li[7]/div[1]/span"));
mouse.moveToElement(address).build().perform();
Thread.sleep(2000L);
String parenthandle = driver.getWindowHandle(); // current window handle
driver.findElement(By.cssSelector("a[href*='goldloan']")).click();
Thread.sleep(5000L);
for (String windowhandle : driver.getWindowHandles()) {
driver.switchTo().window(windowhandle); // switches to next window
}
driver.findElement(By.xpath(".//*[#id='txtFName']")).sendKeys("testing");
driver.close();
driver.switchTo().window(parenthandle);

Related

*** Solved *** Python - Displaying a web site in the same browser tab each time webbrowser.open is called

In Python, I would like to initially open a new browser tab and display a web site. The program will be displaying other png images before cycling back to displaying the web site again. It's like a slide show. I tried to use the webbrowser.open statement displayed below to open the web site with the "New" parameter equaling 1 expecting a new tab not to be opened, but every time webbrowser.open is called, a new tab opens on the Chrome browser.
Can you tell me what coding is needed to ensure once a tab is opened in the browser, additional calls to webbrowser.open won't open new tabs for the web site?
webbrowser.open('https://mawaqit.net/en/abricc-acton-01720-united-states', new=1)
the docs says
webbrowser.open(url, new=0, autoraise=True)
Display url using the
default browser. 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. If autoraise is True, the window is raised if possible (note
that under many window managers this will occur regardless of the
setting of this variable).
Nothing here says that it can replace or close the current tab, so I am afraid that you will have to use something like Selenium instead.
I'm unfamiliar with the webbrowser tool, but using selenium webdriver the following all stays in the same tab.
# import
from selenium import webdriver
from selenium.webdriver.common.by import By
# browser setup
browser = webdriver.Chrome()
browser.implicitly_wait(10)
# open url
browser.get("http://www.google.com")
# click element on page (same tab)
browser.find_element(By.XPATH,"/html/body/div[1]/div[1]/div/div/div/div[1]/div/div[2]/a").click()
# open new URL (same tab)
browser.get("http://www.stackoverflow.com")
Also, some other tools available in selenium are below. These are useful if you want new tabs but need to switch back and forth to "parent" tabs:
# assign tab variables (window_handle = tab apparently)
window = browser.current_window_handle
parent = browser.window_handles[0]
child = browser.window_handles[1]
# switch to new tab
browser.switch_to.window(parent)
I hope that at least some element of this is helpful.

Selenium exits fullscreen when clicking at a link

The Selenium web driver exists fullscreen whenever clicking at a link or opening a new URL.
Any remedies to force fullscreen?
*I am using the fullscreen_window() method.
Google Chrome 91 /
Linux KDE Neon
Let's say when you click on a link like this :
driver.find_element_by_xpath('some xpath').click()
and a new tab opens up :
and then you switch it like this :
driver.switch_to.window(hadles[1])
it would not open screen in full size, you would need to again do this :
driver.maximize_window()

{Python, Selenium} How can I detect browser windows that I'm already using and create a new tab inside said browser window?

I am currently working on a auto log-on for a website, but I found that selenium opens a new browser window each time I run the script. I want to make it so that it opens a new tab in the browser window I am already using. Selenium opens a new browser window, like so:
However, I want it to detect a window I am already using, and place a new tab there, like so:
Can we do this with Selenium for Python or do I need another language?
Sorry if my question is a bit stupid.
Thanks!
There are no stupid questions.
You need to find the reason why your selenium is opening new browser each time you log in.
It is probably because you are initialize webdriver each time you run that method.
Or maybe you use some hooks or tags which are triggering creation of new webdriver.
The code form other user from this site:
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
windows = driver.window_handles
time.sleep(3)
driver.switch_to.window(windows[1])

Fails to perform keys down and hit enter in context menu python selenium

I want to login to a site. Right-click on one of the links and open in New Tab or New Window.
I have searched earlier here and googled around before posting it here. May be I am doing it wrong
button=browser.find_element_by_link_text('Menus');
action=ActionChains(browser)
action.context_click(button).perform() #--> Till here working fine, Right clicks on Menu
action.send_keys(Keys.ARROW_DOWN+Keys.ARROW_DOWN+Keys.ENTER).perform() #--> Not Working
I wouldn't go into that direction as performing context menu clicks will bite you back when you'll be running your Selenium tests in Parallel Mode
Instead of opening context menu and clicking I would rather recommend:
Extract href attribute from the link
Use Window.open() JavaScript function to open the link in the new tab
Use Explicit Wait to wail until number of windows becomes 2
Switch the context to the new tab
Example code:
button = browser.find_element_by_link_text('Menus')
href = button.get_attribute("href")
browser.execute_script("window.open('" + href + "')")
WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
browser.switch_to.window(browser.window_handles[1])

Redirect the selenium link to another link

When I press a button using selenium, it redirects me to a new page. I want my selenium to redirect to same link also. How can I do it?
driver.find_element_by_xpath('//span[contains(text(),"Secure Login")]').click()
is a button I am clicking. It is redirecting me to a new page. I want selenium to get the same link and point to new page.
WebDriver creates and interface between selenium and your web browser. You don't need to handle anything separately in selenium if everything is happening in the same tab and it's just a navigation to a different link.
Though if your link is opening to a different tab, you will need to switch tabs. Before communicating with the elements over the new page.
Why don't you switch to new window, not sure about python but in java its like:
String currentState = driver.getWindowHandle().toString();
for (String handles : driver.getWindowHandles())
{
if (!handles.equalsIgnoreCase(currentState))
{
driver.switchTo().window(handles);
}
}
You can do same in python.

Categories

Resources