Master Selenium Python: Explicit Waits, Scrolling, Zoom, and Advanced Browser Controls
This tutorial demonstrates how to use Selenium with Python for explicit waits, page scrolling, zooming via CSS transforms, retrieving element size and coordinates, disabling JavaScript through Firefox profiles, and configuring manual proxy settings, providing complete code examples for each technique.
Handling Different Wait Scenarios
In Selenium automation, pages may need time to load or you may want to wait for a specific element before proceeding. The WebDriverWait class enables explicit waits by defining a condition and a timeout; if the condition is not met, a TimeoutException is raised.
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://www.example.com/")
timeout = 10
try:
element_present = EC.presence_of_element_located((By.LINK_TEXT, 'Sitemap'))
WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
print("User lookup timed out!")
driver.quit()Scrolling Operations on Web Pages
When testing, you may need to scroll up or down. Using execute_script() with JavaScript commands such as window.scrollTo() achieves the same effect. The example scrolls to the bottom of the page, pauses, then scrolls back to the top.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://www.example.com/")
# Scroll to page bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(10)
# Scroll to page top
driver.execute_script("window.scroll(0, 0);")
sleep(10)
driver.quit()Zoom In and Out with Selenium
To zoom or shrink the page, apply the CSS transform property. For Firefox, the MozTransform property is used. The script first scales the page to 200% and then restores it to 100%.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://www.example.com/")
# Zoom to 200%
driver.execute_script('document.body.style.MozTransform = "scale(2.0)";')
driver.execute_script('document.body.style.MozTransformOrigin = "0 0";')
sleep(10)
# Restore to 100%
driver.execute_script('document.body.style.MozTransform = "scale(1.0)";')
driver.execute_script('document.body.style.MozTransformOrigin = "0 0";')
sleep(10)
driver.quit()Getting Element Size
Locate an element by its ID and use the .size attribute to obtain its width and height.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("http://demos.example.com/test_Menu.html")
search_element = driver.find_element_by_id("createDestroyButton")
print(search_element.size)
driver.quit()Retrieving Element Coordinates
Similar to size, the .location attribute returns the X and Y coordinates of the element.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("http://demos.example.com/test_Menu.html")
search_element = driver.find_element_by_id("createDestroyButton")
print(search_element.location)
driver.quit()Disabling JavaScript via Custom Profile
To test cross‑browser compatibility without JavaScript, modify the Firefox profile preferences. Set javascript.enabled to False and launch the browser with this profile.
from selenium import webdriver
ff_profile = webdriver.FirefoxProfile()
ff_profile.DEFAULT_PREFERENCES['frozen']['javascript.enabled'] = False
ff_profile.set_preference("app.update.auto", False)
ff_profile.set_preference("app.update.enabled", False)
ff_profile.update_preferences()
driver = webdriver.Firefox(ff_profile)
# Verify the setting
driver.get("about:config")Setting Manual Proxy Configuration
When a test requires a specific proxy, import the proxy module, set the type to MANUAL, provide the IP address and port, and attach the configuration to the desired capabilities before launching the browser.
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
proxy_settings = Proxy()
proxy_settings.proxy_type = ProxyType.MANUAL
proxy_settings.http_proxy = "ip_address:port_number"
proxy_settings.socks_proxy = "ip_address:port_number"
proxy_settings.ssl_proxy = "ip_address:port_number"
capabilities = webdriver.DesiredCapabilities.FIREFOX
proxy_settings.add_to_capabilities(capabilities)
driver = webdriver.Firefox(desired_capabilities=capabilities)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
