Found 871 Articles for Automation Testing

How to find an element using the attribute “class name” in Selenium?

Debomita Bhattacharjee
Updated on 08-Nov-2023 00:26:04

22K+ Views

We can find an element using the attribute class name with Selenium webdriver using the locators - class name, css, or xpath. To identify the element with css, the expression should be tagname[class='value'] and the method to be used is By.cssSelector. To identify the element with xpath, the expression should be //tagname[@class='value']. Then, we have to use the method By.xpath to locate it. To locate an element with a locator class name, we have to use the By.className method. Let us look at the html code of an element with class attribute − Syntax WebElement e = driver. findElement(By.className("input")); ... Read More

Check that the element is clickable or not in Selenium WebDriver

Debomita Bhattacharjee
Updated on 06-Apr-2021 07:52:00

18K+ Views

We can check if the element is clickable or not in Selenium webdriver using synchronization. In synchronization, there is an explicit wait where the driver waits till an expected condition for an element is met.To verify, if the element can be clicked, we shall use the elementToBeClickable condition. A timeout exception is thrown if the criteria for the element is not satisfied till the driver wait time.SyntaxWebDriverWait wt = new WebDriverWait(driver, 5); wt.until(ExpectedConditions.elementToBeClickable (By.className("s-buy")));We have to add - import org.openqa.selenium.support.ui.ExpectedConditions and import org.openqa.selenium.support.ui.WebDriverWait statements to implement ExpectedConditions in our code.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 ... Read More

What is the difference between relative and absolute XPath in Selenium?

Debomita Bhattacharjee
Updated on 08-Nov-2023 00:02:51

25K+ Views

We can have two ways of creating an xpath – relative and absolute. The absolute xpath has the complete path beginning from the root to the element which we want to identify.An absolute xpath starts with the / symbol. One drawback with the absolute xpath is that if there is any change in attributes beginning from the root to the element, our absolute xpath will become invalid.The relative xpath starts by referring to the element that we want to identify and not from the root node. A relative xpath starts with the // symbol. It is mainly used for automation ... Read More

How to verify color of a web element in Selenium Webdriver?

Debomita Bhattacharjee
Updated on 03-Apr-2021 11:03:34

21K+ Views

We can verify the color of a webelement in Selenium webdriver using the getCssValue method and then pass color as a parameter to it. This returnsthe color in rgba() format.Next, we have to use the class Color to convert the rgba() format to Hex. Let us obtain the color an element highlighted in the below image. The corresponding color for the element is available under the Styles tab in Chrome browser.The color of the element is also provided in the hex code #797979.SyntaxWebElement t = driver.findElement(By.tagName("h1")); String s = t.getCssValue("color"); String c = Color.fromString(s).asHex();Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import ... Read More

How to open browser window in incognito/private mode using python selenium webdriver?

Debomita Bhattacharjee
Updated on 03-Apr-2021 11:00:57

7K+ Views

We can open a browser window in incognito/private mode with Selenium webdriver in Python using the ChromeOptions class. We have to create an object of the ChromeOptions class.Then apply the method add_argument to that object and pass the parameter -- incognito has a parameter. Finally, this information has to be passed to the webdriver object.Syntaxc = webdriver.ChromeOptions() c.add_argument("--incognito")Examplefrom selenium import webdriver #object of ChromeOptions class c = webdriver.ChromeOptions() #incognito parameter passed c.add_argument("--incognito") #set chromodriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", options=c) driver.implicitly_wait(0.5) #launch URL driver.get("https://www.tutorialspoint.com/tutor_connect/index.php")OutputRead More

How to capture the text from Alert Message in Selenium Webdriver?

Debomita Bhattacharjee
Updated on 03-Apr-2021 11:00:34

3K+ Views

We can capture the text from the alert message in Selenium webdriverwith the help of the Alert interface. By default, the webdriver object has control over the main page, once an alert pop-up gets generated, we have to shift the webdriver focus from the main page to the alert.This is done with the help of the switchTo().alert() method. Once the driver focus is shifted, we can obtain the text of the pop-up with the help of the method switchTo().alert().getText(). Finally we shall use the accept method to accept the alert and dismiss method to dismiss it.Let us take an example ... Read More

How to input text in the text box without calling the sendKeys() using Selenium?

Debomita Bhattacharjee
Updated on 03-Apr-2021 10:57:57

16K+ Views

We can input text in the text box without the method sendKeys with thehelp of the JavaScript Executor. Selenium executes JavaScript commands with the help of the executeScript method.The JavaScript command to be run is passed as parameter to the method. To enter text we shall first identify the input box with the JavaScript method document.getElementById. Then we have to apply the value method on it.Let us enter text Selenium in the below input box −SyntaxJavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript ("document.getElementById('gsc-i-id1').value='Selenium'");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; public class JsEnterText{    public static void main(String[] ... Read More

How to get all options of a dropdown using Python Selenium webdriver?

Debomita Bhattacharjee
Updated on 03-Apr-2021 10:57:32

6K+ Views

We can obtain all options of a dropdown with Selenium webdriver inPython using the method options. It returns a list of the options in the dropdown.Then we have to use the method text, to get the option text.A dropdown is represented by select tag and its available options are represented by the tagname option. To handle dropdown in Selenium, we have to take the help of the Select class.Let us see the html code of a dropdown along with its options – By Subject and By Name.Syntaxl = driver.find_element_by_name("selType") d = Select(l)for opt in d.options -m = opt.textExamplefrom selenium import ... Read More

How to click on image in selenium webdriver Python?

Debomita Bhattacharjee
Updated on 03-Apr-2021 10:52:22

3K+ Views

We can click on an image with Selenium webdriver in Python using the method click. First of all, we have to identify the image with the help of any of the locators like id, class, name, css, xpath, and so on. An image in the html code is represented by the img tagname.Let us see the html code of an image element.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 ImageClk{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");       WebDriver driver = new FirefoxDriver();       //implicit wait   ... Read More

How to find an element using the attribute “name” in Selenium?

Debomita Bhattacharjee
Updated on 03-Apr-2021 10:50:15

11K+ Views

We can find an element using the attribute name with Selenium webdriver using the locators - name, css, or xpath. To identify the element with css, the expression should be tagname[name='value'] and the method to be used is By.cssSelector.To identify the element with xpath, the expression should be //tagname[@name='value']. Then, we have to use the method By.xpath to locate it. To locate an element with a locator name, we have to use the By.name method.Let us look at the html code of an element with name attribute −SyntaxWebElement e = driver. findElement(By.name("q")); WebElement m = driver. findElement(By.xpath("//input[@name = 'q']")); WebElement ... Read More

Advertisements