课程: Test Automation with Selenium WebDriver for Java

Finding an element

- [Instructor] WebDriver provides the findElement() method to locate elements within the DOM. To access this method, we'll say, driver.findElement. This method allows you to indicate the strategy you'd like to use to find an element. Let's visit this website to determine which strategy we should use for an email field. To view the DOM of a webpage, right click on the page and choose Inspect. This will open the browser's developer tools. Choose the select icon, and click the element that you'd like to inspect. Notice this element is now highlighted in the DOM. This input has an id attribute, which is the best strategy for locating an element so we'll use that. I'll copy the ID's value, which is "email". Within findElement(), I'll indicate that I want to find the element by its ID, and passing the value of that ID. So we'll use a By class, say By., and then you specify the locator strategies that you'd like to use. We decided to use id, and then within parentheses and quotation marks, I'm going to paste in the ID for that element. This will return a single web element, which we can store in a variable. So let's say WebElement, and we'll call this emailTextbox =, and now we have an element that we can use and interact with this element using additional WebDriver methods.

内容