You know that the coffee shop was called iCoffee yesterday, but you have no idea what name is it going to have today. There are lots of web applications which are using dynamic IDs for some elements.Īn element with a dynamic ID is like a coffee shop which is changing its name every day. *[ you have a stable ID, you shouldn't bother to use XPath.Īnd if you don't have a stable ID, the XPath that we got is not reliable. In this case, we got the following XPath: The fastest way to get the XPath is from the Chrome console: Usually, an XPath looks something like this: There are a lot of routes that your friend can take to get to that Starbucks. You simply can't avoid having to use XPath for at least some elements.Īn XPath is like a route. Selenium does not have a validation for that, but Endtest does: That's right, Class Names are separated by spaces. HeaderMenu-link d-inline-block no-underline border border-gray-darkīut it actually has 5 different Class Names: The most common mistake done by beginners is extracting all the Class Names instead of just one.Ī novice user might say that the element has the following Class Name: If you tell your friend Meet me at that coffee shop, they might not know which one you're talking about. If Starbucks had a Class Name, it would probably be coffee-shop. If you provide that Class Name, it will only give you a warning, telling you how many elements it found with that Class Name and that the first one will be used. This means that the you won't be able to locate that Username input with the Class Name locator type, since Selenium will throw an error.Įndtest has a more elegant approach here than Selenium. Username = driver.find_element_by_class_name("form-control") We can easily see that our element has the following Class Name:Īnd if we take a closer look at the rest of the inputs from that form, we'll see that all of them have the form-control Class Name. Not so reliable, since a Class Name is usually shared by multiple elements. Username = driver.find_element_by_name("user") 3) Find Element By Class Name We can easily see that our element has the following Name: Just as good as ID, but you won't encounter it as often as you'd expect.Įlements from forms usually have the Name attribute. It's like having only one Starbucks in your city and telling your friends to meet you there, they can't miss. Since an ID should be unique on a page, it's the most reliable locator type that you can use. Username = driver.find_element_by_id("user_login") This means that your line of code will look like this: It seems that our element has the following ID: The structure of your element will be highlighted in the console: In order to get the ID of your element, you just have to right click on your element and click on the Inspect option. We're going to use different locator types to find the Username input: I'll also be making some comparisons between Selenium and Endtest: We'll be using the Github Sign Up page as an example. Keep in mind that asking Selenium to locate an element is like telling someone from out of town how to find a certain coffee shop in your city. The key here is to find the most stable and reliable locator for each element. This article is for anyone who just started using Selenium and wants to know the best ways to find elements.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |