📅  最后修改于: 2023-12-03 14:47:22.063000             🧑  作者: Mango
Selenium Incognito Chrome
Selenium is a popular automation testing tool for web applications that allows developers to test their web application across various browsers and platforms. Google Chrome offers an incognito
mode that allows users to browse the internet without leaving any trace of their activity.
Using Selenium's ChromeDriver
and Options
class, we can open a Chrome browser in incognito
mode and use it for automated testing.
First, we need to download the latest version of ChromeDriver
from the official website and add it to our system's path. We also need to install the selenium
package using pip
.
pip install selenium
The following code snippet shows how to open a Chrome browser in incognito
mode using Selenium and perform a Google search:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com")
search_box = driver.find_element_by_name("q")
search_box.send_keys("Selenium Incognito Chrome")
search_box.submit()
driver.quit()
webdriver
and Options
from selenium.webdriver.chrome
.Options
object called chrome_options
and add an argument --incognito
to it. This argument opens the browser in incognito
mode.webdriver.Chrome
instance and pass the chrome_options
object to it.get()
method to navigate to the Google homepage.find_element_by_name()
method and enter the search query.submit()
method.quit()
method to close the browser and end the session.Using Selenium's ChromeDriver
and Options
class, we can easily open a Chrome browser in incognito
mode and use it for automated testing. This can be very useful for testing web applications that require users to login and perform certain actions while remaining anonymous.