Links, Alerts & Frames/iFrames

Working With Links:

1. How many links present in a webpage.
2. Capture all the links in a webpage
3. Click on the specific link using LINK TEXT or PARTIAL LINK TEXT.

from selenium import webdriver

driver=webdriver.Chrome(executable_path="D:\Selenium\Browsers\chromedriver_win32\chromedriver.exe")
driver.get("http://demo.guru99.com/test/newtours/")
links = driver.find_elements_by_tag_name("a")
print("number of Links:", len(links)) #number of links

for link in links: #capture all the links
print(link.text)

#driver.find_element_by_link_text("REGISTER").click()

driver.find_element_by_partial_link_text("REG").click() #click on the link

driver.quit()

Alerts:
driver.switch_to.alert.accept()
driver.switch_to.alert.dismiss()

Alert is nothing but a kind of pop up by which we cannot identify the element in the pop up.

from selenium import webdriver
import time

driver=webdriver.Chrome(executable_path="D:\Selenium\Browsers\chromedriver_win32\chromedriver.exe")
driver.get("http://testautomationpractice.blogspot.com/")
driver.maximize_window()

driver.find_element_by_xpath("//*[@id='HTML9']/div[1]/button").click()
time.sleep(5)

#driver.switch_to.alert().accept() #closes alert window by clicking on OK button
driver.switch_to.alert.dismiss() #closes alert window by clicking on CANCEL button

driver.quit()


Frames/iFrames:

driver.switch_to.frame(“name”)
driver.switch_to.frame(“id”)

driver.switch_to.default_content()

from selenium import webdriver
import time

driver=webdriver.Chrome(executable_path="D:\Selenium\Browsers\chromedriver_win32\chromedriver.exe")
driver.get("https://www.selenium.dev/selenium/docs/api/java/index.html?overview-summary.html")
driver.maximize_window()

driver.switch_to.frame("packageListFrame") #First Frame
driver.find_element_by_link_text("org.openqa.selenium").click()

driver.switch_to.default_content()
time.sleep(5)
driver.switch_to.frame("packageFrame") #Second Frame
time.sleep(5)
driver.find_element_by_link_text("Alert").click()

driver.switch_to.default_content()
time.sleep(5)
driver.switch_to.frame("classFrame") #Third Frame
time.sleep(5)
driver.find_element_by_xpath("/html/body/header/nav/div[1]/div[1]/ul/li[6]/a").click()

driver.quit()

Popular posts from this blog

WebDriver Basic Commands, Navigational Commands & Conditional Commands

How to Run Tests On Browsers

How to Download & Install Selenium & Python