Java + Selenium element positioning (1) by Id/name/classname

Source: Internet
Author: User

At the beginning of this article, it is also the hardest part to introduce the content of the Web Automation core, which is to locate the elements and to manipulate the elements. The first and most important step in trying to manipulate elements is to find this element, and if the elements are not positioned, then everything is a rip-off. The interface for locating elements in selenium is the Findelement interface. The Findelement interface supports eight ways to find page elements, and this article introduces three simple and convenient methods: by Id,by Name, by ClassName.

We still use Baidu as an example, the way to see the code. I use the browser's own developer tool, which is the window opened by "F12". Click on the icon in the upper right corner of the window and then click on the section of the page where you want to align the action, and the open window will show the code for that part. For example, we want to search for Baidu's input box to locate. You can see the following code when you finish clicking the input box

The code in the blue bar in the diagram is the code of the input box that we clicked on, which expands to see the contents. The red box is framed by some information about the input box. For example, id = "KW" name = "WD" class = "S_ipt".

Similarly we can find the "Baidu" button information. Get id = "SU" class = "bg S_btn".

Here's a place to watch. We can see that the Baidu button has a space in the class. In the by ClassName method, if the class contains a space, it cannot be identified and an error will be caused. So when the class of the element to be positioned contains a space, we cannot use the by ClassName method to find it, and we should change the method.

For example, we'll enter "automated tests" into the input box and look for them. The code shows the following

Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.chrome.ChromeDriver;  Public classPageloadtest { Public Static voidMain (string[] args) {System.setproperty ("Webdriver.chrome.driver", ". \\Tools\\chromedriver.exe"); Webdriver Driver=NewChromedriver (); Driver.get ("Https://www.baidu.com"); Driver.findelement (By.id ("kw")). SendKeys ("Automated Testing"); Enter the input box by using the By ID method

Driver.findelement (By.name ("WD")). SendKeys ("automated Testing");//using the by Name method

Driver.findelement (By.classname ("S_ipt")). SendKeys ("Automated test")//by ClassName method
        Driver.findelement (By.id ("su")). Click ();                            Click on a button
} }

The SendKeys () method and the Click () method that appear in the above code are briefly described here.

The SendKeys () method is a method for entering characters into an element, and the input parameters are generally of type string.

The click () method is the operation that simulates the left mouse button click.

Java + Selenium element positioning (1) by Id/name/classname

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.