In Selenium API, we have different ways to identify an element on Web Page. The following major 8 selectors ways we have, in Selenium, to identify web elements:
By.id
By.name
By.className
By.linkText
By.partialLinkText
By.tagName
By.cssSelector
By.xpath
So, how we use to get the values for above selectors like how we can get the value of id,name etc.? Perform the following steps:
1. Open Google chrome browser.
2. Enter URL "http://newtours.demoaut.com/".
3. Look at the WebElement "UserName" text box.
4. Right click on this WebElement and select "Inspect" option. Now see, on right hand side f page you would be able to see all the information related to UserName WebElement and from there you can copy the attribute value like in this case i can find the value of 'name' attribute i.e. 'userName' which i can use in my selenium script to identify an object like below
driver.findElement(By.name('userName')).sendKeys("mercury");
Find the below snapshots which can help you to understand that how we can get the values for the above selectors
In the above snapshot you can see the below information related to the userName webElement
<input type="text" name="userName" size="10">
So, tag name in the above line is "input" and "type", "name" and "size" are the attributes of input tag. If i use the below code in my script i.e.
driver.findElements(By.tagName("input"))
The above code will not help you to uniquely identify a webElement because at many place the same tag might be used. This statement we use when we have to extract that how many Objects or WebElements are present on page which belongs to the input tag. For example, if someone says that you need to count how many links are available on webPage. In this case we will use anchor tag i.e. <a> which belongs to all the links available on web page.
driver.findElements(by.tagName("a"))
We are using findElements here not findElement with driver object. The reason is this will return a complete list of WebElements present on page which belongs to the tag name.
After this session you would be able to use the below selectors
by.id
by,Name
by.className
by.tagName
by.linkText
by.partialLinkText
In my next tutorial, I will explain how you can identify dynamic elements using xpath and css selector. Till then if you have any doubt please mention in the comment section and I will try to respond quickly on it.
Best Of Luck!
By.id
By.name
By.className
By.linkText
By.partialLinkText
By.tagName
By.cssSelector
By.xpath
So, how we use to get the values for above selectors like how we can get the value of id,name etc.? Perform the following steps:
1. Open Google chrome browser.
2. Enter URL "http://newtours.demoaut.com/".
3. Look at the WebElement "UserName" text box.
4. Right click on this WebElement and select "Inspect" option. Now see, on right hand side f page you would be able to see all the information related to UserName WebElement and from there you can copy the attribute value like in this case i can find the value of 'name' attribute i.e. 'userName' which i can use in my selenium script to identify an object like below
driver.findElement(By.name('userName')).sendKeys("mercury");
Find the below snapshots which can help you to understand that how we can get the values for the above selectors
In the above snapshot you can see the below information related to the userName webElement
<input type="text" name="userName" size="10">
So, tag name in the above line is "input" and "type", "name" and "size" are the attributes of input tag. If i use the below code in my script i.e.
driver.findElements(By.tagName("input"))
The above code will not help you to uniquely identify a webElement because at many place the same tag might be used. This statement we use when we have to extract that how many Objects or WebElements are present on page which belongs to the input tag. For example, if someone says that you need to count how many links are available on webPage. In this case we will use anchor tag i.e. <a> which belongs to all the links available on web page.
driver.findElements(by.tagName("a"))
We are using findElements here not findElement with driver object. The reason is this will return a complete list of WebElements present on page which belongs to the tag name.
After this session you would be able to use the below selectors
by.id
by,Name
by.className
by.tagName
by.linkText
by.partialLinkText
In my next tutorial, I will explain how you can identify dynamic elements using xpath and css selector. Till then if you have any doubt please mention in the comment section and I will try to respond quickly on it.
Best Of Luck!
No comments:
Post a Comment