🚀 Advanced Selenium Concepts in Python
Modern web applications use complex technologies like Shadow DOM, dynamic JavaScript rendering, secure connections, and automation restrictions. Selenium provides advanced features to handle these challenges effectively.
⚡ JavaScript Executor
JavaScript Executor allows execution of JavaScript code directly in the browser.
driver.execute_script("alert('Hello Selenium');")
# Click using JS
driver.execute_script("arguments[0].click();", element)
# Scroll
driver.execute_script("window.scrollBy(0,500);")
---
🌑 Handling Shadow DOM
Shadow DOM elements are not accessible using normal locators.
shadow_host = driver.find_element(By.CSS_SELECTOR, "custom-element")
shadow_root = driver.execute_script("return arguments[0].shadowRoot", shadow_host)
element = shadow_root.find_element(By.CSS_SELECTOR, "button")
element.click()
---
👻 Headless Browser Testing
Headless testing runs browser without UI.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
---
🧩 Handling Captcha (Concept Only)
Captcha is designed to block automation. Selenium should not bypass Captcha.
Ethical Solutions:- Disable Captcha in test environment
- Use test tokens
- Use Captcha mock services
- Manual verification step
⚙ Browser Options & Capabilities
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--incognito")
options.add_argument("--disable-notifications")
options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=options)
---
🍪 Cookies Handling
# Get cookies
cookies = driver.get_cookies()
# Add cookie
driver.add_cookie({"name":"test","value":"123"})
# Delete cookies
driver.delete_all_cookies()
---
🔐 SSL Certificate Handling
options = Options()
options.add_argument("--ignore-certificate-errors")
options.add_argument("--allow-insecure-localhost")
driver = webdriver.Chrome(options=options)
---
- Use JS Executor carefully
- Avoid Captcha automation
- Use headless for CI/CD
- Use browser options wisely
- Handle cookies for sessions
- Maintain security compliance
Advanced Selenium concepts enable automation of modern complex web applications. Mastering these topics makes you a professional-level Selenium Automation Engineer.
No comments:
Post a Comment