Versions used while doing this blog post, (These are the prerequisites to automate chrome browser. Browser and driver versions should match)
selenium python version – 4.5.0
chrome browser version – 105
chrome driver version – 105 (Download link : https://chromedriver.chromium.org/downloads) [download this to a path in project directory]
Size and location coordinates of a webelement can be captured using selenium 4 by following below steps:
1. Instantiate (creation of object) driver object with webdriver.Chrome() (This step opens the chrome browser)
driver = webdriver.Chrome("chromedriver path need to be passed")
2. Now, next steps of maximizing the browser, setting implicit timeout for driver object and launch of expected URL can be carried out in default window
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("https://www.google.com")
3. Add an explicit wait that waits till the expected element appears [Note : WebDriverWait(driver object, Duration method) can be used as WebDriverWait(driver object, long) got deprecated now]
logoElement = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "img[alt='Google']")))
4. Get the size and location of the element using rect method
print(logoElement.rect)
Sample code for reference
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
driver = webdriver.Chrome("chromedriver path need to be passed")
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("https://www.google.com")
logoElement = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "img[alt='Google']")))
print(logoElement.rect)
driver.quit()