Python Forum

Full Version: I use switch_to.window, how to keep edge at a minimized ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm going to run edge in a minimized window, not headless. However, running the following code restores the window to its original state and the window is active. If I don't use driver.switch_to_window and can't locate element, I just want to keep running at a minimum and left me alone.

Driver. Find_element_by_xpath (" / / a [@ href = "http://www.cdot.in/home.htm"] "). Click window_after = () driver.window_handles[1] driver.switch_to.window(window_after) driver.find_element_by_link_text("ATM").click()
It seems like you're trying to automate a web interaction using the Selenium library in Python. However, you're facing issues with window handling and element locating. Here's what I understand from your description, along with some suggestions to improve your code:

Minimized Window Handling: To run Microsoft Edge in a minimized window but not headless, you can set the browser window's dimensions to a smaller size (e.g., 800x600) using driver.set_window_size(width, height) after creating the driver instance.

Window Handling: You're trying to interact with different windows, but it seems like the code you provided doesn't handle the windows correctly. If you want to keep the original window minimized and work with a new window that's opened after clicking a link, you need to switch to the new window using driver.switch_to.window(window_handle).

Element Locating: It's crucial to wait for elements to be present and interactable before attempting to interact with them. Using WebDriverWait can help ensure that elements are ready to be interacted with.

Here is an example code for your clerification:-

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Create the driver instance
driver = webdriver.Edge()  # Or use other browser drivers like Chrome, Firefox, etc.

# Minimize the window
driver.set_window_size(800, 600)

# Open the URL
driver.get("your_url_here")

# Click on the first link
link = driver.find_element(By.XPATH, "//a[@href='http://www.cdot.in/home.htm']")
link.click()

# Switch to the new window
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)

# Wait for the element to be clickable
wait = WebDriverWait(driver, 10)
atm_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "ATM")))

# Click on the "ATM" link
atm_link.click()

# Now you can interact with elements on the new window
# ...

# Close the browser
driver.quit()
Please replace "your_url_here" with the actual URL you're working with. Also, make sure to adjust the element locating strategies (By.XPATH, By.LINK_TEXT, etc.) to match the structure of the webpage you're automating. Additionally, use appropriate wait strategies to ensure that the elements are ready for interaction.