Showing posts with label Selenium Webdriver Python. Show all posts
Showing posts with label Selenium Webdriver Python. Show all posts

Get HTML Source of WebElement in Selenium Webdriver using Python

Get HTML Source of WebElement in Selenium Webdriver using Python,in this lesson you will get complete selenium python code to print or get html source of webelement with the help of innerHtml attribute to get source of the content of the element or outerHtml for source with the current element.

Below is the code:


element.get_attribute('innerHTML')


import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        elem = driver.find_element_by_xpath("//*")
  source_code = elem.get_attribute("outerHTML")
  

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()
 

If you want to save it to a file,use below code.
 
 
f = open('c:/htmlfile.html', 'w')
f.write(source_code.encode('utf-8'))
f.close()

Selenium WebDriver Page Object Model Framework Introduction

Page Object Model Framework in Selenium WebDriver has come to be very popular Selenium WebDriver test automation framework in the software industry and many IT companies are following this framework because of its easy to write test scripts, Test script maintenance and reduces the duplication of code.

The main merit of Page Object Model in Selenium WebDriver is that if the Web UI changes for any module pages, it don’t require to change any Selenium WebDriver Test Scripts, we just need to change only the Test Page code within the page objects. 


Page Object Model in Selenium WebDriver


Example:


public class LoginPage{

public static WebDriver driver;
private By pageTile = By.xpath("//input[@title='Selenium WebDriver']");
private By homePage=By.id("home");
private By singInPage=By.id("login");
private By uname=By.id("userName");
private By password = By.id("password");

}




In the above code, we've identified the Element locators and defined in the after the class. In this way we can achieve readability of test scripts and we can easily identify Web Element locators and change the Element Locators as per required only in page object classes.
Page Object model in Selenium WebDriver is useful in preparing the AUT functionalities or Reusable components of a Webpage which we want to automate in a separate Test class. Let’s consider as Login Page.

As per Selenium WebDriver ORG Page Object Model is


"Within your web app’s UI there are areas that your tests interact 
with. A Page Object simply models these as
objects within the test code.This reduces the amount of duplicated code
 and means that if the UI changes,the fix need only be applied in one place."

For the above page I’m creating simile class as LoginPage.class. In this class we’ll write reusable methods to call in another class to implement the Test cases.

Let’s consider entry page as Bing Search Page which will have many options like Search, Sign In, Images, Videos, Maps, News Languages, MSN, Outlook.com links. Here as per user action it will navigates to respective web pages to achieve this all functionalities that we want to automate should have reusable methods/components for each web-page.

Now as our main Entry Page is Bing Home page user can navigate to respective pages by clicking on any link from the Bing Search page. Whenever users are navigating to respective pages, we have to return particular page objects. Otherwise Return the current page object as this action doesn't navigate to another page signified by another Page Object.

The Page Object model advantages.

1. Clean Code between test code and page specific code such as locators and layout.
2. Single Object Repository for all functionalities, No Duplication of codes.

In above situation this allows you to perform modifications only in UI because of CR in current requirements or changes in UI Designs.

As per Selenium WebDriver WIKI Page

There is a PageFactory in the support package that provides support for this pattern, and helps to remove some boiler-plate code from your Page Objects at the same time.

Page Object Model in Selenium WebDriver Step by Step Guide

Page Object Model in Selenium WebDriver Projects has become crucial Automation Framework in different organization and many software Testers are implementing Selenium Page Object Model in their project why because it is easy to maintain Test Scripts, easy to understand the Test Execution, easy to maintain Page Object Repository and last but not least reduces the duplication of code.

Recommended Articles

  1. Write Test Cases PASS OR FAIL in Excel with SELENIUM
  2. Create Object Repository in Selenium Project.
  3. Page Factory Framework Implementation Examples.
  4. Start APPIUM server programmatically.


Page Object Model in Selenium WebDriver

The main advantage of Page Object Model in Selenium WebDriver is easy to maintain Page Objects, easy to avoid duplicate of Code. Duplicate code in Selenium cause duplicate of different Functionalities with this duplicate code which results in multiple time repetition of duplicate element locators. You will learn How to implement Page Object Model in Selenium using Simple Examples as explained below. In this examples I’m using simple Login.java class, so that you can implement for other classes in your Selenium Projects.

In this Page Object Model, you need to create two packages

One for Page Objects Second one for Page Test Cases Implementation as below example.

How to IMPLEMENT?

STEP 1: First Create JAVA Project
STEP 2: Configure required JAR files in Build Path
STEP 3: Create two packages under src folder
STEP 4: One Package for Page Object (Object Repository)
STEP 5: Second Package for Page Test Classes.


Login.java

Above class created under page Objects package and code will be as follows
package com.selenium.pageObjects; 
import org.openqa.selenium.*; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement;
 
public class LoginPage {
 
        private static WebElement element;
 
    public WebElement setUserName(WebDriver driver){
 
         element = driver.findElement(By.id("uName"));
 
         return element;
 
         }
 
     public WebElement setPassword(WebDriver driver){
 
         element = driver.findElement(By.id("password"));
 
         return element;
 
         }
 
     public WebElement clickOnSignin(WebDriver driver){
 
         element = driver.findElement(By.id("submit"));
 
         return element;
 
         }
 
}

WebDriver is being passed as a Method Arguments for each methods, so that Selenium is able to locate the Web Page elements on the specified browser using driver object. In above code element is returned, because method is not a void that is the reason we should return the values inside the method to use in another class or somewhere.so that an Action can be performed on elements. Method is declared as Public WebElement, so that method can be used in any other methods without instantiate the class.Now let’s create Page Test Case class under page Actions package as below for Login Page.

LoginTest.java
package com.selenium.Actions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.selenium.pageObjects.LoginPage ;

public class LoginTest {

     private static WebDriver driver = null;
 LoginPage login=new LoginPage();

//Main Method
   public static void main(String[] args) {

 System.setProperty("webdriver.gecko.driver","E:\\geckodriver\\geckodriver.exe");
     driver = new FirefoxDriver();
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
     driver.get("http://www.yourloginurl.com");

     // Use page Object library now

     login.setUserName(driver).sendKeys("rajesh_3434");
     login.setPassword(driver).sendKeys("makeindia");
     login.clickOnSignin(driver).click();

     System.out.println(" User Login Successfully,My Account screen is open");

      driver.quit();

     }

}





Now create testng.xml file to run your Test Actions classes as specified in your testng.xml file,Just right click on it and Run As – TestNG Tests. 

 You will see Test script Execution on Firefox Browser as provided login credentials. 

 Thank you for reading my blog ,Please share this post in case you feel informative and provide your valuable comments on this post.

Selenium Webdriver Using Python - Complete Tutorial Guide

Selenium supports Python language bindings and can be developed with Selenium Test Scripts for testing.

Python is easy matched to other programming languages, having extreme fewer verbose.
Python language is easy to learn and easy to write the scripts using Python Language.

The Python Language APIs enables you to link with the browser from end to end using Selenium.Selenium guides the standard Python commands to different browsers.

Selenium WebDriver Test Scripts can be run in different browsers and on different operating Systems such as


Firefox
Chrome
IE

What is Python?

Python is a high-level object oriented programming language or scripting Language with dynamic semantics. 


Selenium Webdriver Using Python - Complete Tutorial Guide
Selenium Webdriver Using Python - Complete Tutorial Guide


Selenium WebDriver Using Python - Complete Tutorial Guide

1.Python is a Simple Language
2.Easy to Learn
3.Easy to Read
4.Easy to Maintain


Sample Python Example:


print("Hello World!")
ready = True
if ready:
print("Hello World!")

Why to Choose Python Language in Selenium WebDriver

Below different points explains why Python language is better for Selenium WebDriver scripting, those are

1.Extensive Support Libraries
2.Open Source
3.Learning Ease and better Support
4.User Friendly Data Structure
5.Python uses indentation

Selenium WebDriver Python bindings provides a better API to write functional/acceptance test scripts using Selenium WebDriver. Through Selenium WebDriver Python API we can access all functionalities of Selenium WebDriver in a natural way.

Selenium WebDriver Python Concepts:

Download Python Bindings for Selenium WebDriver
Install Python Language.
Download Drivers for Browsers
First Selenium Python Script
Selenium Web Pages Interaction using Python
Locating Elements
Python Wait Commands
Selenium Page Objects
Selenium WebDriver API

How to Get HTML Source of WebElement in Selenium WebDriver using Python

How to Get HTML Source of WebElement in Selenium WebDriver using Python,While working with Selenium WebDriver scripting using any language bindings whether it is a Python,Java or Ruby,we require to verify Expected Results with Actual results to check Test case is PASS or FAIL.In this selenium WebDriver using python post you will learn Get HTML Source of WebElement in Selenium WebDriver using Python languages.


Get HTML Source of WebElement in Selenium WebDriver using Python


I'm using python bindings to create Selenium Webdriver Test scripts,till now you've learn Selenium Webdriver Test scripting with JAVA but coming to Python language it is completely different.

We can read WebElement source with the help of innerHTML attribute to get the Element Source of the Content.

Using chromeDriver to open Google Chrome Browser.

Different Language code to Get HTML Source of WebElement in Selenium WebDriver,Please use below different code to use in Selenium webdriver Test Scripts.

PYTHON:

element.get_attribute('innerHTML')


PHP:

$elem.getAttribute('innerHTML');


JAVA:

elem.getAttribute("innerHTML");


C#:

element.GetAttribute("innerHTML");


RUBY:

element.attribute("innerHTML")


JS:

element.getAttribute('innerHTML');