Selenium Python Webdriver Tutorial - Locating Elements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to a selenium master selenium Python webdriver video tutorial in this video tutorial I will teach you how to locate elements in using selenium Python webdriver there are various strategies to locate elements in a page you can use the most appropriate one for your case selenium provides the following messages to locate elements in a page so first one is find element by ID when an ID is present you can find an element using its ID second find element by name you can find an element using its name find element by XPath when an element doesn't tell ID name you can use the XPath to identify or locate the element find element by linked text when a link is present you can find the link by its link text find element by partial link text when the link text is long you can use partial text to locate the element find element by tag name you can find an element by its tag name tag is basically the HTML tag in the page resource find element by class name you can find an element using its class name when the class is unique and find element by CSS selector this is basically a find an element using it as cascaded stylish it selector so series we use find element each method will be used to identify one element at a time now let's see an example so as explained in previous video tutorial let's first import the selenium webdriver so from selenium import webdriver now webdriver is imported now let's define a driver using a browser instance so we can use Chrome driver equal to web driver that so when you press that the Python shell into the sense we will display a list of actions you can choose this is a useful feature of Python IDE so you can use the existing method you don't need to remember what method to use so here we select chrome now hit enter so Chrome browser opened now let's navigate to the testing site driver dot we get get is a URL HTTP demo dot Mahara dot org now the browser navigate to the demo dot Mahara dot org website let's maximize the browser window so driver that you can choose the maximized window method and hit enter now the window is maximized now let's see how we can identify elements using different strategies first let's observe the page resource on this page I right-click on the user name field and click on inspect element so let's change the stock side to the dog to bottom okay now let's observe this username so if you look at here username has ID and a name and it also has class so now let's find out how we can use ID or name now let's define user name by its ID so username equal to driver let me minimize this Python IDE so we can see the result on the screen driver dot so scroll down by ID and it's ID was low again underscore go again underscore user name now this element is defined now let's enter a text so user name that we can use the method send keys so we type in student one so student one is displayed in the text field so now let's clear the text use their name dot so clear now the text is clear as we see on the element definition this element has name in its tag so we can use name let's use username equal to driver dot so find element by name its name was low again user name now the element is defined and let's type in another word user name dot send the keys let's say this time is type in selenium master so the word is displayed now let's clear it user name dot so clear now that word is cleared so we have already used ID name now let's use X Pass so under user name field if we right-click and inspect element we can find out its XPath so we can use input and use its ID we form an XPath so we can identify this element now let's look at how we can identify zealand by XPath so user name equals driver dot find element by X pass so X pass all this starts with two forward slashes and input ID attribute has low again low again use their name now this Express is defined now we type in username dot let's say sandy Keys let's type in XPath expression so that XPath expression is displayed here so now let's clear that username clear so we cleared that expression so now let's try the link text on this page we see Mahara Vicky Mahara home page and Mahara user 1 manual so now let's find the Mahara Vicky and click on it so right click inspect element and scroll the page so Mahara wiki has a link text Mahara wiki so now it's finds element by its link text so we can define the element Mahara wiki link equals to driver so find element by link text and it's text was Mahara wiki so now that element is found now let's click on this Mahara wiki Mahara wiki link that so we click on it now it clicked and it opened the Mahara wiki page so now let's go back to the previous page we can use driver dot back so back means it will now get to the previous page so now let's find the Mahara user manual by using the partial link text so if you right click on here and inspect the element so this is Mahara user manual let's find this by using Mahara user partial link text we close it so Mahara user equals driver dot find element by partial link text so we can use Mahara user now the element is defined so we can get the elements attribute let's find out what attribute the element has this element the attribute edge RAF that is the link so we can return that so Mahara use their dot yet attribute get attribute means we get one attribute of the HTML tag so here it returned the link so we already explained ID name Xbox link text and partial link text now let's find out the tag name so if the tag is unique we can find the element by its tag name so let's see on this page when we look at the resource there's our tag footer so we can get the footer on the page by using the tag so now let's find out so footer equals to driver dot find element by tag name and tag name is footer so this element is find now let's print out all the text so footer dot text so this printed out the terminus and conditions privacy statement about contact us so on this page we have the terms and conditions prior statement about contact us these are a footer so now let's look at the last one class name and CSS selector so for the class name we can find this element like register lost username and password if you right-click on the inspect element so this element has a class and this is Logan related length so we can get that using the specific class name now let's close this and find that element by class so low again related length equal to driver dot find element by class by class name so this class name is it is low again - late it - links so we hit enter now let's print out the text so low again related links that text so it printed out register lost username and password now let's find out how to look at element by using CSS selector so for the CSS selector we need to use a tag and its class and also some other attributes so let's inspect the logo on this page and inspect element so under body so this is the logo logo is defined by the link tag a and class logo so now and it has this SRC underneath or the href the link so let's find this element by using cascading style sheet method so here we define site logo element equals to driver find element by CSS selector so a tag its class is logo so now site logo is defined now site logo dot let's say get attribute h r EF it's a hyperlink and it printed out so the link is they move that Mahara dot org and we can also use some other method let's say site logo so is displayed so this method will return whether the element is visible to our user so hit enter it's visible and yet return true so as explained in this video tutorial you can look at web element using find element by ID find element by name find element by XPath find element by link text find element by partial link text find element by tag name find element by last name find element by CSS selector if you have any questions how to use the locators in your script you can always check out some documentation on the Python web driver website and if you have any more questions you can always email two services at selenium master calm thanks for watching you
Info
Channel: Selenium Master
Views: 34,529
Rating: 4.7292309 out of 5
Keywords: Selenium Python Webdriver Tutorial, Selenium Locating Elements, Selenium Python Locating Elements, Element Locators in Selenium, Locate Element By Id, Locate Element By Name, Locate Element By Xpath
Id: mQ7-mPJYJ5A
Channel Id: undefined
Length: 22min 58sec (1378 seconds)
Published: Thu Nov 19 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.