添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • 0. Beginners, start here
  • 1. System Setup
  • Installing programming language dependencies
  • Installing Selenium dependencies
  • 2. First selenium script
  • Create a new project and include dependencies
  • Writing and running code
  • 3. Identifying elements in the page
  • Inspect attributes like id, class, name etc of an element
  • Introduction to XPath locators
  • XPath functions
  • CSS Selectors
  • 4. Interacting with the Elements in the page
  • Locating element by using findElement
  • Performing actions like click, type, select etc
  • Getting the list of elements and Child Element
  • 5. Waits
  • Implicit Wait
  • Explicit wait
  • 6. Handling Alert dialog, Popup windows and Frames
  • Handling Alert dialog
  • Handling Popup windows
  • 7. Interacting with the Browser
  • Running test on different browsers
  • Taking the screenshot of the page
  • Executing JavaScript through JavascriptExecutor
  • 8. Advanced User Interactions
  • Introducing Actions and moving the mouse over an element
  • Performing drag and drop operations
  • Automating Keyboard press events
  • 9. Selenium Grid
  • Introduction to Selenium Grid & Setting up hub and nodes
  • RemoteWebDriver and DesiredCapabilities
  • Running tests on SauceLabs' cloud machines
  • 10. Selenium Pro Tips
  • How to connect Selenium to an existing browser that was opened manually?
  • How to disable insecure password warning in Firefox for Selenium?
  • WebDriver Exceptions
  • How to interact with shadow DOM in Selenium?
  • > Click here to watch Video Tutorials
  • > TestNG - a testing framework
  • TestNG | Introduction & Setup
  • Object-oriented programming in JavaScript
  • Introduction to Object Oriented Programming | JavaScript & Object Oriented Programming | Part 1
  • Setting up environment | JavaScript & Object Oriented Programming | Part 2
  • Object Literals | JavaScript & Object Oriented Programming | Part 3
  • Deep dive into Object Literals | JavaScript & Object Oriented Programming | Part 4
  • Factory Functions | JavaScript & Object Oriented Programming | Part 5
  • Constructor Functions | JavaScript & Object Oriented Programming | Part 6
  • Built-in Constructor Functions in JavaScript | JavaScript & Object Oriented Programming | Part 7
  • How to implement Abstraction in JavaScript | JavaScript & Object Oriented Programming | Part 8
  • Adding, removing and iterating Object Properties | JavaScript & Object Oriented Programming | Part 9
  • Getters and Setters in JavaScript | JavaScript & Object Oriented Programming | Part 10
  • Prototypes in JavaScript | JavaScript & Object Oriented Programming | Part 11
  • Understanding var, let and const in JavaScript
  • Automation Practice | Beginner
  • Automation Practice | First Selenium Script
  • Automation Practice | Handling Waits
  • Automation Practice | WebTable
  • About Us
  • 0. Beginners, start here
  • 1. System Setup
  • Installing programming language dependencies
  • Installing Selenium dependencies
  • 2. First selenium script
  • Create a new project and include dependencies
  • Writing and running code
  • 3. Identifying elements in the page
  • Inspect attributes like id, class, name etc of an element
  • Introduction to XPath locators
  • XPath functions
  • CSS Selectors
  • 4. Interacting with the Elements in the page
  • Locating element by using findElement
  • Performing actions like click, type, select etc
  • Getting the list of elements and Child Element
  • 5. Waits
  • Implicit Wait
  • Explicit wait
  • 6. Handling Alert dialog, Popup windows and Frames
  • Handling Alert dialog
  • Handling Popup windows
  • 7. Interacting with the Browser
  • Running test on different browsers
  • Taking the screenshot of the page
  • Executing JavaScript through JavascriptExecutor
  • 8. Advanced User Interactions
  • Introducing Actions and moving the mouse over an element
  • Performing drag and drop operations
  • Automating Keyboard press events
  • 9. Selenium Grid
  • Introduction to Selenium Grid & Setting up hub and nodes
  • RemoteWebDriver and DesiredCapabilities
  • Running tests on SauceLabs' cloud machines
  • 10. Selenium Pro Tips
  • How to connect Selenium to an existing browser that was opened manually?
  • How to disable insecure password warning in Firefox for Selenium?
  • WebDriver Exceptions
  • How to interact with shadow DOM in Selenium?
  • > Click here to watch Video Tutorials
  • > TestNG - a testing framework
  • TestNG | Introduction & Setup
  • Object-oriented programming in JavaScript
  • Introduction to Object Oriented Programming | JavaScript & Object Oriented Programming | Part 1
  • Setting up environment | JavaScript & Object Oriented Programming | Part 2
  • Object Literals | JavaScript & Object Oriented Programming | Part 3
  • Deep dive into Object Literals | JavaScript & Object Oriented Programming | Part 4
  • Factory Functions | JavaScript & Object Oriented Programming | Part 5
  • Constructor Functions | JavaScript & Object Oriented Programming | Part 6
  • Built-in Constructor Functions in JavaScript | JavaScript & Object Oriented Programming | Part 7
  • How to implement Abstraction in JavaScript | JavaScript & Object Oriented Programming | Part 8
  • Adding, removing and iterating Object Properties | JavaScript & Object Oriented Programming | Part 9
  • Getters and Setters in JavaScript | JavaScript & Object Oriented Programming | Part 10
  • Prototypes in JavaScript | JavaScript & Object Oriented Programming | Part 11
  • Understanding var, let and const in JavaScript
  • Automation Practice | Beginner
  • Automation Practice | First Selenium Script
  • Automation Practice | Handling Waits
  • Automation Practice | WebTable
  • About Us
  • In the Previous Tutorial , we learned how to handle timeout issues in Selenium. In this tutorial, we will learn how to handle Alert windows.

    What is an Alert window?

    You must have noticed that on clicking some links/buttons, the page displays an alert window that asks the user to confirm if she or he wants to do this action by clicking on OK/Cancel or Yes/No buttons. Some of them display text like this – “Are you sure you want to perform this action?”. You can check it out yourself by going to the Automation Practice page. When you’ll click on the link – ‘Click Me to get Alert’, the page will prompt this alert:

    System.setProperty("webdriver.chrome.driver", "C:\\teachmeselenium\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://cosmocode.io/automation-practice");
    from selenium import webdriver
    chrome_driver_path = "C:\teachmeselenium\chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver_path)
    driver.get("https://cosmocode.io/automation-practice")
    System.setProperty("webdriver.chrome.driver", "C:\\teachmeselenium\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://cosmocode.io/automation-practice");
    driver.findElement(By.linkText("Click Me to get Alert")).click();
    Alert alert = driver.switchTo().alert();
    String strAlertText = alert.getText();
    System.out.println(strAlertText);
    alert.accept();
    //alert.dismiss();
    driver.quit();
    from selenium import webdriver
    chrome_driver_path = "C:\teachmeselenium\chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver_path)
    driver.get("https://cosmocode.io/automation-practice")
    #Click on the link
    driver.find_element_by_link_text("Click Me to get Alert").click()
    #Switch to alert window
    alert = driver.switch_to_alert()
    #Get Text
    print(alert.text)
    #Accept alert
    alert.accept()
    #Reject alert
    # alert.dismiss()
    driver.quit()

    This tutorial was so short and crispy. Wasn’t it? In the Next Tutorial , we will explore real-world challenges like handling popup windows.

    Do we have something by which we can print in a dialog box while the script is executing.
    I mean in C# dot net we have MessageBox.show(" Script is at the middle");

    Do we have such thing in eclipse…. ?

    Also would like to know in this tutorial though we have used some drivers of selenium but GUI(front end) we have used only eclipse… Till now in most of the tutorials i have seen we have used eclipse and selenium just for finding elements.

    When we are going to use selenium GUI

    Regards
    Gaurav Khurana

    http://www.Udzial.com
    Udzial Means share

    Reply

    1. That is not specific to eclipse. It is just an IDE over which you write java code. There are couple of ways in java to display message box. However it is not as simple as C# or Vbscript. Google "Java Message Box" and you will some ways…swing or JOptions

    2. You are getting confused buddy. The GUI that you are talking about is Selenium IDE, where we can record browser actions and play it back. We can also write some steps there. But these are specific to Selenium IDE. Selenium IDE, RC, WebDriver all these are different selenium variants. What we are following in tutorial is Selenium WebDriver that is the latest selenium variant. And eclipse is used as an IDE to code in Java. It is not specific to Selenium. If you were writing Selenium Webdriver code in C# you would have used Visual C#/Visual Studio IDE that are standard C# ediors.

    Reply

    Adding to previous comments… Selenium doesn't have its own editor to write code. you would need to depends on the editor specific to the language you are using as you know you can write selenium code in multiple languages… For Java you can use eclipse, netbeans etc… C#, Visual C#/Visual Studio…. Python, some other editros… You cannot write Selenium WebDriver or RC code in Selenium IDE. Its purely record-playback tool.

    Reply

    Ok got it. Can you cover Selenium IDE tool also a little bit as you will be knowing details for using the record playback tool. or some source where we can learn that too,
    I tried recording and playback and it worked great,, but would like to know the details further

    Regards
    Gaurav Khurana
    http://www.udzial.com
    Udzial Means Share

    Reply

    Gaurav, Unfortunately covering Selenium IDE is not in scope of this tutorial for now.. as we are focusing the latest one WebDriver.. In real life projects you would hardly use Selenium IDE…. However for knowledge and fun purpose you can take help from official selenium documentation http://docs.seleniumhq.org/docs/02_selenium_ide.jsp

    Reply

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.JUnit4;
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;

    @RunWith(JUnit4.class)
    public class Handling {

    @Test
    public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get(" http://www.cosmotechies.com/teachmeselenium/p/automation-practice.html&quot ;);

    driver.findElement(By.linkText("Click Me to get Alert")).click();

    Alert alert = driver.switchTo().alert();

    String strAlertText = alert.getText();
    System.out.println(strAlertText);
    alert.accept();

    driver.quit();
    }
    }

    Reply

    You are ahead of this tutorial's scope dude 🙂 . JUnit and TestNG are yet to come into picture.

    Reply

    Thanks for providing good article on "handling javascript alert window". http://reditblog.blogspot.in/

    Reply

    Hi Shadab,

    When I try to run the code as is, I get the following exception:

    Exception in thread “main” org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)…

    Since I’m essentially getting a ‘not yet present’ error, do you suppose I should include a ‘waitTillElementIsDisplayed’ statement in the code and is this possible with an alert?

    Many thanks for this beautiful tutorials you have put together here. Much appreciated my friend!

    Reply

    Hi Ola

    Thanks for bringing it to our notice. Please use the updated base URL –
    http://www.teachmeselenium.blogspot.com/p/automation-practice.html
    You would not face issue with it.

    The reason you were getting exception is because on clicking the link ‘Click Me to get Alert’, no alert was getting opened. Since we migrated the website to different hosting we are facing this kind of issue. I think I should be using older links for automation practice till we fix it out.

    Reply

    I’m trying to build a automation test on the site https://pre.niio.com/singin
    when entering a wrong email, password and clicking login a popup appears. I’m trying the following code but I’m getting a “no such alert” exception.
    Alert alert = driver.switchTo().alert();
    alert.accept();
    why the popup is not recognize?
    pleas assist

    Reply

    Hi Meir,

    The popup that you see on that page is not a JavaScript Alert. It is a regular page element part of the current page. If you right click over the OK button and click inspect you can see its propoerties. You can use the right properties to click that button.

    Reply
  • Inspect attributes like id, class, name etc of an element
  • Introduction to XPath locators
  • XPath functions
  • CSS Selectors
  • Locating element by using findElement
  • Performing actions like click, type, select etc
  • Getting the list of elements and Child Element
  • Implicit Wait
  • Explicit wait
  • 6. Handling Alert dialog, Popup windows and Frames
  • Handling Alert dialog
  • Handling Popup windows
  • Running test on different browsers
  • Taking the screenshot of the page
  • Executing JavaScript through JavascriptExecutor
  • Introducing Actions and moving the mouse over an element
  • Performing drag and drop operations
  • Automating Keyboard press events
  • Introduction to Selenium Grid & Setting up hub and nodes
  • RemoteWebDriver and DesiredCapabilities
  • Running tests on SauceLabs’ cloud machines
  • How to connect Selenium to an existing browser that was opened manually?
  • How to disable insecure password warning in Firefox for Selenium?
  • How to interact with shadow DOM in Selenium?
  • TestNG | Introduction & Setup
  • Introduction to Object Oriented Programming | JavaScript & Object Oriented Programming | Part 1
  • Setting up environment | JavaScript & Object Oriented Programming | Part 2
  • Object Literals | JavaScript & Object Oriented Programming | Part 3
  • Deep dive into Object Literals | JavaScript & Object Oriented Programming | Part 4
  • Factory Functions | JavaScript & Object Oriented Programming | Part 5
  • Constructor Functions | JavaScript & Object Oriented Programming | Part 6
  • Built-in Constructor Functions in JavaScript | JavaScript & Object Oriented Programming | Part 7
  • How to implement Abstraction in JavaScript | JavaScript & Object Oriented Programming | Part 8
  • Adding, removing and iterating Object Properties | JavaScript & Object Oriented Programming | Part 9
  • Getters and Setters in JavaScript | JavaScript & Object Oriented Programming | Part 10
  • Prototypes in JavaScript | JavaScript & Object Oriented Programming | Part 11
  • Understanding var, let and const in JavaScript
  • Automation Practice | Beginner
  • Automation Practice | First Selenium Script
  • Automation Practice | Handling Waits
  • Automation Practice | WebTable
  • About Us
  •