We can find elements in a web page with the help of JavaScript. We can also validate the elements returned by the JavaScript methods in the browser Console (Pressing F12). JavaScript methods to find elements are −getElementsByTagnameTo obtain a collection of elements with the matching tagname passed as parameter to the method. If there are no matching elements, an empty collection is returned.Syntaxdocument.getElementsByTagName("") To get the first matching element, document.getElementsByTagName("")[0]getElementsByNameTo obtain a collection of elements with the matching value of name attribute passed as parameter to the method. If there are no matching elements, an empty collection is returned.Syntaxdocument.getElementsByName("") To ... Read More
A hyperlink on a page is identified with the anchor tag. To click a link, we can use the link text locator which matches the text enclosed within the anchor tag.We can also use the partial link text locator which matches the text enclosed within the anchor tag partially. NoSuchElementException is thrown if there is no matching element found by both these locators.SyntaxWebElement t =driver.findElement(By.partialLinkText("Refund")); WebElement m =driver.findElement(By.linkText("Refund Policy"));Let us make an attempt to click a link Refund Policy on the below page −ExampleCode Implementation with linkTextimport 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 LnkClick{ ... Read More
We can get the typed text from a textbox with Selenium webdriver. Firstly, we have to enter text in the text box (after being identified with any locators) with the help of the sendKeys method.Then apply the method getAttribute to obtain the text entered in that field and pass the parameter value to that method. Let us make an attempt to obtain the value entered in the Google search box −SyntaxWebElement m = driver.findElement(By.name("q")); String st = m.getAttribute("value");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 GetTextTyped{ public static void main(String[] args) { ... Read More
We can get the text found within the span tag with Selenium webdriver.The text of a web element can be captured with the method getText. Let us see an example of an element having the text - © Copyright 2021. All Rights Reserved enclosed within the span tag.SyntaxWebElement l = driver.findElement(By.xpath("//p/span")); String s = l.getText();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 SpanText{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); ... Read More
To specify ENTER button functionality in Selenium webdriver we have to use the method sendKeys. To simulate pressing the ENTER button, we have to add the statement import org.openqa.selenium.Keys to our code.Then pass the parameter – Keys.RETURN or Keys.ENTER to the sendKeys method.Let us make an attempt to press the ENTER button after entering some text in the Google search input box −ExampleCode Implementation with Keys.ENTERimport 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.Keys; public class EnterOperation{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); ... Read More
We can get the text from a list of all web elements with the same class name in Selenium webdriver. We can use any of the locators like the class name with method By.className, xpath with method By.xpath, or css with method By.cssSelector.Let us verify a xpath expression //h2[@class='store-name'] which represents multiple elements that have the same class name as store-name. If we validate this in Console with the expression - $x("//h2[@class='store-name']"), it yields all the matching elements as shown below:Also, since we need to obtain multiple elements, we have to use the findElements method which returns a list. We ... Read More
We can use the Selenium webdriver scripts in Firefox (versions>47) with the help of the geckodriver.exe executable file. First, we have to download this file from the following link −https://github.com/mozilla/geckodriver/releasesOnce we navigate to the mentioned URL, we have to click a link based on the operating system (Windows, Linux or Mac) we are presently using. As the download gets over, a zip file is created. We have to extract the zip file and store the geckodriver.exe file in a desired location.Then, we have to configure the path of the geckodriver.exe file using the System.setProperty method along with creating an object ... Read More
We can select data from a datepicker using Selenium webdriver. A datepicker in a calendar can be designed in numerous ways on the web UI. Based on the UI, we have to design our test.A calendar may have a dropdown for selection of day, month or year. It may also contain forward and backward navigation to move up and down in the dates or any other design. The below example shows a calendar having a datepicker. Let us make an attempt to select the date 03/02/2021(2nd March, 2021) date from the below calendar −In the above html code, we can ... Read More
We can use the JavaScript Executor in Selenium to click an element.Selenium can execute JavaScript commands with the help of the method executeScript.Sometimes while clicking a link, we get the IllegalStateException, to avoid this exception, the JavaScript executor is used instead of the method click. The parameters to be passed to the executeScript method to click an element are - arguments[0].click(); and the web element locator.SyntaxWebElement m=driver.findElement(By.linkText("Company")); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].click();", m);Let us click the link Company on the below page −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.JavascriptExecutor; public class ClickLnkJS{ public ... Read More
Declaring an ArrayIn order to declare an array, you follow the syntax give below −Syntaxtype array_name[array_size];Exampleschar buf[500]; int new_array[200];Accessing elements of the arrayThe array element numbering starts from 0. The element can be accessed by specifying the index of the element in square brackets against the name of the array. For instance −int second_element = new_array[1];Getting length of arrayThe length of the array can be accessed using the sizeof() function.For example, int buf_len = sizeof(buf);Please note that the sizeof() function returns the number of bytes, and not the number of elements. If you have an int array, and int is ... Read More