Common webpage operations using Selenium
Selenium provides so many capabilities to perform test automation which makes it easy to perform any webpage based task. This page has all the reusable code blocks which are required for commonly used webpage operations during test automation with Selenium webdriver and Java such as: highlight webelement in selenium, mouseover in selenium or mouse hover in selenium using Actions, extract css, take a screenshot, upload file, scroll up ,down, switch frames,, etc. Some methods may use javascript with selenium.
Contents
- 1 highlight webelement in selenium
- 2 How to use mouseover in Selenium
- 3 Extract CSS Attribute Of An Element
- 4 Execute A JavaScript Statement On Page
- 5 Upload A File On A Page
- 6 Scroll Up, Down Or Anywhere On A Page
- 7 Get HTML Source Of A Element On Page
- 8 Switch Between Frames In Java Using Webdriver
- 9 How to Type in Selenium without using sendKeys() method
- 10 Click in Selenium if button or radio button is disable
highlight webelement in selenium
How to use mouseover in Selenium
Actions Actions = New Actions(Driver); WebElement MenuElement = Driver.FindElement(By.Id("Menu-Element-Id")); Actions.MoveToElement(MenuElement).MoveToElement(Driver.FindElement(By.Xpath("Xpath-Of-Menu-Item-Element"))).Click();
Extract CSS Attribute Of An Element
String TextColor = Driver.FindElement(By.Id("Id123")).GetCssValue("Color");
Execute A JavaScript Statement On Page
JavascriptExecutor Jsx = (JavascriptExecutor) Driver; Jsx.ExecuteScript("Alert('Hi')");
Upload A File On A Page
String FilePath = "Path\To\Filefor\Upload"; JavascriptExecutor Jsx = (JavascriptExecutor) Driver; Jsx.ExecuteScript("Document.GetElementById('FileName').Value='" + FilePath + "';");
Scroll Up, Down Or Anywhere On A Page
JavascriptExecutor Jsx = (JavascriptExecutor) Driver; //Vertical Scroll - Down By 100 Pixels Jsx.ExecuteScript("Window.ScrollBy(0,100)", ""); //Vertical Scroll - Up By 55 Pixels (Note The Number Is Minus 55) Jsx.ExecuteScript("Window.ScrollBy(0,-55)", ""); //Horizontal Scroll - Right By 20 Pixels Jsx.ExecuteScript("Window.ScrollBy(20,0)", ""); //Horizontal Scroll - Left By 95 Pixels (Note The Number Is Minus 95) Jsx.ExecuteScript("Window.ScrollBy(-95,0)", "");
Get HTML Source Of A Element On Page
JavascriptExecutor Jsx = (JavascriptExecutor) Driver; String ElementId = "Element-Id"; String Html =(String) Jsx.ExecuteScript("Return Document.GetElementById('" + ElementId + "').InnerHTML;");
Switch Between Frames In Java Using Webdriver
WebElement FrameElement = Driver.FindElement(By.Id("Id-Of-Frame")); Driver.SwitchTo().Frame(FrameElement);
How to Type in Selenium without using sendKeys() method
FirefoxDriver driver=new FirefoxDriver(); // Maximize the window driver.manage().window().maximize(); // Open applicatiion driver.get("enter your application URL"); // This will execute JavaScript in your script ((JavascriptExecutor)driver).executeScript("document.getElementById('some id').value='mukesh'");
Click in Selenium if button or radio button is disable
FirefoxDriver driver=new FirefoxDriver(); // Maximize the window driver.manage().window().maximize(); // Open applicatiion driver.get("enter your application URL"); // This will execute JavaScript in your script ((JavascriptExecutor)driver).executeScript("document.getElementById('enter your element id').click()");
Want us to add
more code ?
more code ?