Get Webpage Details Using JavaScript Executor in Selenium

Debomita Bhattacharjee
Updated on 06-Apr-2021 08:13:33

980 Views

We can get details of a web page like url, title, domain name of webpage using JavaScript Executor in Selenium webdriver. Selenium can execute JavaScript commands with the help of the executeScript method. The command to be executed is passed as a parameter to that method.SyntaxTo get the page title, JavascriptExecutor j = (JavascriptExecutor) driver; String s = j.executeScript("return document.title;").toString();To get the current URL, String p = j.executeScript("return document.URL;").toString();To get the domain, String d = j.executeScript("return document.domain;").toString();Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; public class JavaScrptScope{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", ... Read More

Use JavaScript Executor to Interact with Web Elements in Selenium

Debomita Bhattacharjee
Updated on 06-Apr-2021 08:12:12

16K+ Views

We can use JavaScript Executor to click and enter data to a web element in Selenium webdriver. Selenium can run JavaScript commands with the help of the executeScript method.To click an element, the parameters to the executeScript method are - arguments[0].click(); and the webelement locator.SyntaxWebElement l = driver.findElement(By.className("gsc-input")); JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("arguments[0].click();", l);Then to enter data to the edit box, the parameter passed to the executeScript method is – web element locator.valueSyntaxj.executeScript("document.getElementsByName('gsc-i-id1')[0].value= 'Java'");Let us try to click on the below edit box and enter data into it.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; ... Read More

Get Page Source in Browser Using Selenium

Debomita Bhattacharjee
Updated on 06-Apr-2021 08:11:33

12K+ Views

We can get page source as it is in browser using Selenium webdriver using the getPageSource method. It allows us to obtain the code of the page source.SyntaxString p = driver.getPageSource();We can also obtain the page source by identifying the body tag with the help offindElement method and then apply the getText method on it. The parameter By.tagName is passed as a parameter to the findElement method.SyntaxWebElement l= driver.findElement(By.tagName("body")); String p = l.getText();ExampleCode Implementation with getPageSourceimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; public class PgSrc{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", ... Read More

Get Attribute Value of Web Element in Selenium using Java or Python

Debomita Bhattacharjee
Updated on 06-Apr-2021 08:10:28

3K+ Views

We can get the attribute value of a web element with Selenium webdriver using the method getAttribute and then pass the attribute for which we want to get the value as a parameter to that method.In an html code, an element is defined with attributes and its values in a key-value pair. Let try to get the class – heading, for the below element on the page −SyntaxWebElement t =driver.findElement(By.xpath("//li[text()='About Tutorialspoint']")); String s = t.getAttribute("class");Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; public class AttribtValue{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", ... Read More

Delete All Tags of Azure Resource Using PowerShell

Chirag Nagrekar
Updated on 06-Apr-2021 08:06:13

855 Views

To delete all the tags of the Azure VM using PowerShell, we need to use the Remove-AZTag command.To delete the azure resource tags, we need its resource ID but before that, it is always better to take azure resource tags backup. You can search the question on the TutorialPoints site or google, Site − TutorialsPoint.com How to Export the Azure VM tags using PowerShell?To delete the azure Resource Tags, PS C:\> $vm = Get-AzVM -Name Testmachine2k16 PS C:\> Remove-AzTag -ResourceId $vm.Id -VerboseTo remove the azure resourceGroup tags, PS C:\> $rg = Get-AzResourceGroup AnsibleTestRG PS C:\> Remove-AzTag -ResourceId $rg.ResourceId -VerboseTo remove ... Read More

Drawbacks of Selenium WebDriver

Debomita Bhattacharjee
Updated on 06-Apr-2021 08:05:06

575 Views

Some of the prominent drawbacks of Selenium are listed below −Has assistance for only web oriented applications.Time consuming to configure the environment unlike the paid tools like UFT.Has no features provided by Test Management tools like ALM or UFT.New characteristics introduced in Selenium, sometimes do not work as expected.Has no in-built test report generation. It has to be integrated with TestNG/JUnit for reports.Cannot be integrated with Test Management tools like ALM or UFT.Cannot be used for verifying images.Has no record and play feature, hence building a test script needs considerable time and effort.Cannot be used for verifying mobile applications. We ... Read More

Retrieve CSV File Headers Using PowerShell

Chirag Nagrekar
Updated on 06-Apr-2021 08:04:58

6K+ Views

To retrieve the CSV file headers using PowerShell, we need to use the hidden property PSObject once we use to import the CSV file. We have a CSV file stored at the C:\temp\VMTags.csv and we need to retrieve its headers. The CSV file is as below.ABCDForPatching_DayApplicationOwnerAnsibleSundaySecretTagChiragImporting the CSV file, PS C:\> $csv = Import-Csv C:\Temp\VMTags.csv PS C:\> $csv For    Patching_Day Application Owner ---    ------------ ----------- ----- Ansible    Sunday    SecretTag ChiragExampleAccessing hidden property, PS C:\> $csv.psobjectOutputWe need to use Properties to get our values. We will get all the headers here.PS C:\> $csv.psobject.Properties | Select Name ... Read More

Find Element Containing Specific Text in Selenium WebDriver with Python

Debomita Bhattacharjee
Updated on 06-Apr-2021 08:04:36

6K+ Views

We can find an element that contains specific text with Selenium webdriver in Python using the xpath. This locator has functions that help to verify a specific text contained within an element.The function text() in xpath is used to locate a webelement depending on the text visible on the page. Another function contains() in xpath is used to locate a webelement with the sub-text of actual text visible on the page.Let us try to identify the element having the specific text - Privacy Policy.Syntaxl = driver.find_element_by_xpath("//a[text()='Privacy Policy']") m = driver.find_element_by_xpath("//a[contains(text(), 'Privacy')]")Examplefrom selenium import webdriver #set chromodriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") ... Read More

Open Link in New Tab of Chrome Browser using Selenium WebDriver

Debomita Bhattacharjee
Updated on 06-Apr-2021 08:03:34

2K+ Views

We can open a link in the new tab of Chrome browser using Selenium webdriver using the methods Keys.chord and sendKeys. The method Keys.chord is used to send multiple keys simultaneously as parameters.To open a new tab, the Keys.CONTROL and Keys.ENTER are passed as parameters to the Keys.chord. Finally, the Keys.chord is passed as a parameter to the sendKeys.Let us click on the Jobs links in a new tab, highlighted in the below image −SyntaxString l = Keys.chord(Keys.CONTROL, Keys.ENTER); driver.findElement(By.xpath ("//a[@title='Job @ Tutorials Point']")).sendKeys(l);Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Keys; public class ElementLocator{    public ... Read More

Find Element and FindElements in Selenium

Debomita Bhattacharjee
Updated on 06-Apr-2021 08:02:47

2K+ Views

The methods findElement and findElements are used to identify elements on the webpage. Both these methods can be used with locators like id, css, class, name, xpath, css, link text, tagname and partial link text.The method findElement is used to identify an element which matches with the locator (used with By object) passed as a parameter to that method. If there is no matching element, then NoSuchElementException is thrown.The method findElements is used to identify a list of elements which matches with the locator (used with By object) passed as a parameter to that method. If there is no matching ... Read More

Advertisements