top of page

learner'sBlog

What is the role of the `getText` method and how this method is utilized to fetch and display the text content from webelement

# Import necessary libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# Create a Service object for the Chrome browser
service_obj = Service()

# Initialize the Chrome WebDriver with the created Service
driver = webdriver.Chrome(service=service_obj)

# Open the specified website
driver.get("https://the-internet.herokuapp.com/")

# Maximize the browser window
driver.maximize_window()

# Set an implicit wait of 3 seconds to allow elements to be found
driver.implicitly_wait(3)

# Find and print the text of the first <h1> element on the page
h1_element_text = driver.find_element(By.TAG_NAME, "h1").text
print(h1_element_text)

# Close the browser window
driver.quit()

17 views0 comments

What is the approach for dealing with multiple tabs in a web browser using Selenium in Python?


import time
from selenium import webdriver

# Create a new instance of the Chrome WebDriver
driver = webdriver.Chrome()

# Open the first tab and navigate to a website
driver.get("https://the-internet.herokuapp.com/")
time.sleep(3)
# Open a new tab using JavaScript (Ctrl+T shortcut)
driver.execute_script("window.open('', '_blank');")
time.sleep(3)
# Switch to the second tab
driver.switch_to.window(driver.window_handles[1])
driver.get("https://the-internet.herokuapp.com/windows")
print(driver.window_handles[1])

# Open another tab using JavaScript
driver.execute_script("window.open('', '_blank');")

# Switch to the third tab
driver.switch_to.window(driver.window_handles[2])
driver.get("https://the-internet.herokuapp.com/windows/new")
print(driver.window_handles[2])
# Close the third tab
driver.close()
# Switch back to the second tab
driver.switch_to.window(driver.window_handles[1])

# Perform actions in the second tab

# Close the remaining tabs
driver.close()
time.sleep(3)
driver.switch_to.window(driver.window_handles[0])
print(driver.window_handles[0])
driver.close()

# Quit the WebDriver
driver.quit()

7 views0 comments
bottom of page