Python Selenium Continue Script When Element is Found
Introduction to selenium find element
The selenium find element is the command that is used to identify a web element within a web page in a unique way, and it returns the first matching web element; also if suppose the multiple web elements are newly discovered by using the specific location else it will throw the error like no such element not found so that it accepts only the unique elements take in the By object as the parameter and it will return the object type as the web element and it will either using javascript language and its event like click, enter, select, choose, etc.
Selenium Find Element Overviews
Selenium is one of the tools for testing a website's data loading and regression. This Interaction with a web page requires the driver to locate the web element, and it either initiates a JavaScript event for triggering the actions performed by the user end. The find element called by using the xpath with return the element which matches the xpath locator has the argument passed as the separate parameter for utilizing this method.
It will return the list of elements for checking the list size with 0 index for losing the functionality with javascript elements. Mainly it will focus on the web driver the drives are supported using the browsers like Chrome, Firefox, etc. So the drivers will get the data using the get() method while passing the URL as the parameter for executing the web driver in the web-based application URL with corresponding browsers.
How to use the Find Element command?
This find element is mainly used for to identified the unique set of web elements within the web page. It contains any set of web locator elements using id and name we can pass the datas to identified the datas with default methods like findElement() with default parameters like findElement(By.id(<element ID)) and findElement(By.name(<element-name)) for each set of commands and default methods will vary their behaviors for to automate the test in the web-based application.
Not only the ID and Name, we can pass the element in find command using the xpath; by using this xpath, we must specify the first name tab and then right-click the mouse and choose >> inspect tab for inspecting the values. On inspecting the web-based elements, it must be shown as the input tag first and then the attributes like class and id for to construct the XPath, which in turn and it will be located on the name field. If we use By.Name() method will be used as another type of way like locating the web element to look at the same issue with the id attribute.
Syntax:
WebElement web = driver.findElement(By.LocatorStrategy("path value"))
The locator strategy be supported for following type of values,
ID, Name, Xpath these are the keyword types of locating the values in the web element,
We used following demo application for to test the automation script,
https://www.website.com/sign-in/?source=SC&country=IN
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class New {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.manage().window().maximize();
dr.get("https://www.website.com/?source=SC&country=IN");
dr.findElement(By.id("memberFunction")).click();
WebElement wb = dr.findElement(By.id("username"));
wb.sendKeys("ramansiva57@gmail.com");
WebElement wb1 = dr.findElement(By.id("password"));
wb1.sendKeys("ramansiva57@gmail.com");
dr.findElement(By.id("signin-submit2")).click();
}
}
We used different types of Xpath like the absolute XPath also same like the name attribute element it will be started using the root element of the web page and other documents also if we write the absolute xpath before that, we must start the single forward slash for to performing the string and URLs in the same variable. It is the most dependent way for until we must find the desired element to pass the parameter in the Xpath. The above xpath is the different types generally it comes under the two different flavors 1. Absolute Xpath and 2. Relative Xpath, we use some UI tag elements like radio button, scroll option, checkbox, text box, etc. these are some UI tag elements with the specified feature. The find element command will always return the single element that will matches the first element within the specified web page of the application. If suppose the tester will use this element, it executes the loop condition if it does not exist, which means it will throw the NoSuchElementException occurred if the element is not found on the page.
Find Id and Name
The selenium finds keyword will use with many sets of sub-attributes according to the ID, and the name will be mostly used in the selenium script. If suppose we used Find by ID, the web developers using a non-unique set of Ids, and it generates random or dynamic Ids used for the MVC framework application.
Find By ID- Example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class New {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.get("http://facebook.com");
WebElement wb= dr.findElement(By.id("email"));
wb.sendKeys("ramansiva57@gmail.com");
WebElement wb1= dr.findElement(By.id("pass"));
wb1.sendKeys("12345");
}
}
Output:
In the above example, we used findElement(By.id("id")); we can pass the input values using the sendKeys("values") method with input parameters. Before that, we must set the chrome drivers in the setProperty() method and make sure we must be download the latest chrome driver version in the machine. WebElement, WebDriver, ChromeDriver, By these are some default classes which imported on the java package class. System.setProperty() is the default method for calling the chrome driver name with the specified path like mapping technique key-value pairs.
Find By Name-Example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class New {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.get("https://google.com/");
WebElement wb= dr.findElement(By.name("q"));
wb.sendKeys("Good Night");
}
}
Output:
In the above example, we used Google search engine as the get() method parameter, which passes the value, and then we can use the findElement(By.name()) for getting the google search engine textbox parameter name like string value "Good Night" we can also write some customization code using xpath and other default features. We can use the default methods with each and every separate parameter is passed to the web element. The google search button is used to search the advanced characters with filter strings that also we can utilise in the web elements classes and their methods.
Conclusion
In the selenium framework, we used a lot of default packages like classes, methods, keywords, etc. Like that findElement By Id and findElement By name are the methods for finding the web elements in the Locators by using the selenium scripts. It accepts javascript, java, python, C#, etc. it supports most of the language for testing the different applications.
Recommended Articles
This is a guide to selenium find element. Here we discuss How to use the Find Element command along with the examples and outputs. You may also have a look at the following articles to learn more –
- Selenium Load Testing
- Selenium Software
- Assertions in Selenium
- Components of Selenium
Source: https://www.educba.com/selenium-find-element/
0 Response to "Python Selenium Continue Script When Element is Found"
Publicar un comentario