YouTip LogoYouTip

Selenium Browser Operator

Selenium can not only operate web page elements, but also control the browser's own behavior, such as managing windows, handling pop-ups, operating Cookies, executing JavaScript, etc.\n\nThe following are commonly used browser operations in Selenium and their descriptions:\n\n| **Operation Type** | **Method** | **Description** |\n| --- | --- | --- |\n| **Open new window** | `execute_script("window.open('URL');")` | Use JavaScript to open a new window. |\n| **Switch window** | `switch_to.window(window_handle)` | Switch to the specified window handle. |\n| **Close window** | `close()` | Close the current window. |\n| **Maximize window** | `maximize_window()` | Maximize the browser window. |\n| **Set window size** | `set_window_size(width, height)` | Set the size of the browser window. |\n| **Fullscreen mode** | `fullscreen_window()` | Set the browser window to fullscreen mode. |\n| **Open webpage** | `get(url)` | Open the specified URL. |\n| **Forward and backward** | `back()` and `forward()` | Return to the previous page or forward to the next page. |\n| **Refresh page** | `refresh()` | Refresh the current page. |\n| **Handle Alert popup** | `switch_to.alert` combined with `accept()` or `dismiss()` | Handle Alert popup, click confirm or cancel. |\n| **Handle Confirm popup** | `switch_to.alert` combined with `accept()` or `dismiss()` | Handle Confirm popup, click confirm or cancel. |\n| **Handle Prompt popup** | `switch_to.alert` combined with `send_keys()` and `accept()` or `dismiss()` | Handle Prompt popup, enter text then click confirm or cancel. |\n| **Add Cookie** | `add_cookie(cookie_dict)` | Add the specified Cookie. |\n| **Get Cookie** | `get_cookie(name)` or `get_cookies()` | Get the specified Cookie or all Cookies. |\n| **Delete Cookie** | `delete_cookie(name)` or `delete_all_cookies()` | Delete the specified Cookie or all Cookies. |\n| **Execute JavaScript** | `execute_script(script)` | Execute the specified JavaScript code. |\n| **Get JavaScript return value** | `execute_script("return ...")` | Execute JavaScript and return the result. |\n| **Switch to iframe** | `switch_to.frame(iframe_element)` | Switch to the specified iframe. |\n| **Switch back to main page** | `switch_to.default_content()` | Switch back to the main page. |\n\n* * *\n\n## 1. Handling browser popups (alert, confirm, prompt)\n\nIn web applications, popups (such as alert, confirm, prompt) are common user interaction methods. Selenium provides the `Alert` class to handle these popups.\n\n### 1.1 Handling alert popup\n\n`alert` popups are usually used to display a message, and users can only click the "OK" button to close the popup.\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\nfrom selenium.webdriver.common.alert import Alert\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Open webpage\n\n driver.get("https://example.com")\n\n# Trigger alert popup\n\n driver.execute_script("alert('This is an alert popup');")\n\n# Switch to alert popup\n\n alert = Alert(driver)\n\n# Get popup text\n\nprint(alert.text)\n\n# Click "OK" button to close popup\n\n alert.accept()\n\n# Close browser\n\n driver.quit()\n\n### 1.2 Handling confirm popup\n\n`confirm` popups are usually used to confirm operations, and users can choose "OK" or "Cancel".\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\nfrom selenium.webdriver.common.alert import Alert\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Open webpage\n\n driver.get("https://example.com")\n\n# Trigger confirm popup\n\n driver.execute_script("confirm('This is a confirm popup');")\n\n# Switch to confirm popup\n\n alert = Alert(driver)\n\n# Get popup text\n\nprint(alert.text)\n\n# Click "OK" button\n\n alert.accept()\n\n# Or click "Cancel" button\n\n# alert.dismiss()\n\n# Close browser\n\n driver.quit()\n\n### 1.3 Handling prompt popup\n\n`prompt` popups are usually used to get user input, and users can enter text and choose "OK" or "Cancel".\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\nfrom selenium.webdriver.common.alert import Alert\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Launch browser\n\n driver = webdriver.Chrome()\n\n# Open webpage\n\n driver.get("https://example.com")\n\n# Trigger prompt popup\n\n driver.execute_script("prompt('This is a prompt popup');")\n\n# Switch to prompt popup\n\n alert = Alert(driver)\n\n# Get popup text\n\nprint(alert.text)\n\n# Enter text\n\n alert.send_keys("User input")\n\n# Click "OK" button\n\n alert.accept()\n\n# Or click "Cancel" button\n\n# alert.dismiss()\n\n# Close browser\n\n driver.quit()\n\n## 2. Switching windows and tabs\n\nIn web applications, users may open multiple windows or tabs. Selenium provides the `switch_to.window()` method to switch windows or tabs.\n\n### 2.1 Switching windows\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Open first window\n\n driver.get("https://example.com")\n\n# Open second window\n\n driver.execute_script("window.open('https://example.org');")\n\n# Get all window handles\n\n window_handles = driver.window_handles\n\n# Switch to second window\n\n driver.switch_to.window(window_handles)\n\n# Operate in second window\n\nprint(driver.title)\n\n# Switch back to first window\n\n driver.switch_to.window(window_handles)\n\n# Close browser\n\n driver.quit()\n\n### 2.2 Switching tabs\n\nSwitching tabs is similar to switching windows, because each tab is an independent window handle.\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Open first tab\n\n driver.get("https://example.com")\n\n# Open second tab\n\n driver.execute_script("window.open('https://example.org');")\n\n# Get all tab handles\n\n tab_handles = driver.window_handles\n\n# Switch to second tab\n\n driver.switch_to.window(tab_handles)\n\n# Operate in second tab\n\nprint(driver.title)\n\n# Switch back to first tab\n\n driver.switch_to.window(tab_handles)\n\n# Close browser\n\n driver.quit()\n\n## 3. Handling iframe\n\n`iframe` is another webpage embedded in a webpage. Selenium provides the `switch_to.frame()` method to switch to an iframe.\n\n### 3.1 Switching to iframe\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Open webpage\n\n driver.get("https://example.com")\n\n# Switch to iframe\n\n iframe = driver.find_element_by_tag_name("iframe")\n\n driver.switch_to.frame(iframe)\n\n# Operate in iframe\n\nprint(driver.page_source)\n\n# Switch back to main page\n\n driver.switch_to.default_content()\n\n# Close browser\n\n driver.quit()\n\n### 3.2 Switching back to main page\n\nAfter operating in the iframe, you can use the `switch_to.default_content()` method to switch back to the main page.\n\n## Example\n\n# Switch back to main page\n\n driver.switch_to.default_content()\n\n## 4. Browser Cookies operations\n\nSelenium provides methods such as `get_cookies()`, `add_cookie()`, `delete_cookie()` to operate browser Cookies.\n\n### 4.1 Getting Cookies\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Open webpage\n\n driver.get("https://example.com")\n\n# Get all Cookies\n\n cookies = driver.get_cookies()\n\nprint(cookies)\n\n# Close browser\n\n driver.quit()\n\n### 4.2 Adding Cookie\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Open webpage\n\n driver.get("https://example.com")\n\n# Add Cookie\n\n driver.add_cookie({"name": "test","value": "123"})\n\n# Get all Cookies\n\n cookies = driver.get_cookies()\n\nprint(cookies)\n\n# Close browser\n\n driver.quit()\n\n### 4.3 Deleting Cookie\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\n# Set the correct driver path\n\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\n\n options = webdriver.ChromeOptions()\n\n driver = webdriver.Chrome(service=service, options=options)\n\n# Open webpage\n\n driver.get("https://example.com")\n\n# Add Cookie\n\n driver.add_cookie({"name": "test","value": "123"})\n\n# Delete Cookie\n\n driver.delete_cookie("test")\n\n# Get all Cookies\n\n cookies = driver.get_cookies()\n\nprint(cookies)\n\n# Close browser\n\n driver.quit()\n\n## 5. Executing JavaScript code\n\nSelenium provides the `execute_script()` method to execute JavaScript code.\n\n### 5.1 Executing JavaScript code\n\n## Example\n\nfrom selenium import webdriver\n\nfrom selenium.webdriver.chrome.service import Service as ChromeService\n\n# Set the correct driver path\n\n service = ChromeSer
← Selenium File OperatorSelenium Ele Operator β†’