1)getText():it fetches the element text from a webpage & stores in a string variable
syntax:
String txt=driver.findElement(By.locator("locatorvalue")).getText();
2)getAttribute("attributename"):it fetches the given attribute value
String val=driver.findElement(By.locator("locatorvalue")).getAttribute("attributename");
Q)how to fetch the tooltip of an element?
Ans)tooltip is available in 'title' attribute
String tooltip=driver.findElement(By.locator("locatorvalue")).getAttribute("title");
Q)how can you fetch the url of the link? -->link url will be found in 'href' attribute.
String url=driver.findElement(By.locator("locatorvalue")).getAttribute("href");
Q)how can you fetch the image source ?-->image source will be found in 'src' attribute.
String imgsrc=driver.findElement(By.locator("locatorvalue")).getAttribute("src");
3)getCssValue("cssproperty"):it fetches the css property value
String cssval=driver.findElement(By.locator("locatorvalue")).getCssValue("cssproperty");
csspropertyNames:color,font-size,font-family,text-decoration,background-color
Example:
font-family: Arial,Helvetica,sans-serif;
font-size: 16px;
letter-spacing: 0em;
line-height: 1.6em;
font-weight: 400;
font-style: normal;
letter-spacing: .01em;
line-height: 1.8em;
text-transform: none;
color: rgba(23,23,23,.75);
text-decoration: underline;
Code Snippets Scenarios:
Q)how can you fetch the particular element color?
String c=driver.findElement(By.localtor("locatorvalue")).getCssValue("color");
Q)how can you find out particular element is udnerline or not?
String ud=driver.findElement(By.localtor("locatorvalue")).getCssValue("text-decoration");
4)getLocation():it fetches the element position in the webpage
Point p=driver.findElement(By.locator("locatorvalue")).getLocation();
SOP("x coordinate:"+p.getX()+"y cooardnate"+p.getY());
5)getSize():it fetches the height n width of the element
Dimension d=driver.findElement(By.locator("locatorvalue")).getSize();
SOP(d.getHeight()+" "+d.getWidth());
6)getTagName():it fetches the tag name for the element
String tag=driver.findElement(By.locator("locatorvalue")).getTagName();
syntax:
String txt=driver.findElement(By.locator("locatorvalue")).getText();
2)getAttribute("attributename"):it fetches the given attribute value
String val=driver.findElement(By.locator("locatorvalue")).getAttribute("attributename");
Q)how to fetch the tooltip of an element?
Ans)tooltip is available in 'title' attribute
String tooltip=driver.findElement(By.locator("locatorvalue")).getAttribute("title");
Q)how can you fetch the url of the link? -->link url will be found in 'href' attribute.
String url=driver.findElement(By.locator("locatorvalue")).getAttribute("href");
Q)how can you fetch the image source ?-->image source will be found in 'src' attribute.
String imgsrc=driver.findElement(By.locator("locatorvalue")).getAttribute("src");
quick update on Selenium - getAttribute() method:
SeleniumDev recently reverted the deprecated warning for 'getAttribute method' (4.33 onwards).
But getDomProperty() & getDomAttribute() - methods are relevant when you care about strict property vs attribute distinction.
Check this feature log details in github: https://lnkd.in/g64REA5d
Important points:
=================
1. getDomProperty(String name)
Returns: The current DOM property value.
Reflects: JavaScript-updated values (live values).
Example: For an <input value="initial">, if JavaScript later updates it (element.value = "changed"), this method will return "changed".
Use case: When you want the live, JavaScript-updated value of a property (like .value, .checked, .selectedIndex, etc.).
More Property Examples:
-element.innerText,
-element.textContent,
-element.innerHTML,
-element.baseURL,
-element.checked = false,
-etc...
=================
2. getDomAttribute(String name)
Returns: The original HTML attribute value as it was in the DOM.
Reflects: Static attribute values (as written in the HTML).
Example: For <input value="initial">, even if JS changes it later, this still returns "initial".
Use case: When you want the raw, initial attribute from the HTML, without any changes caused by JavaScript.
================
3. getAttribute(String name)
Returns:
First: the DOM property value (live JS value) if it exists.
Else: the DOM attribute value (original HTML value).
Else: null.
Behavior:
Acts as a hybrid of the above two.
Commonly used for convenience when you don’t care about strict property vs attribute distinction.
Use case: When you want a convenient way to retrieve either the property or attribute, preferring the updated/live value.
String cssval=driver.findElement(By.locator("locatorvalue")).getCssValue("cssproperty");
csspropertyNames:color,font-size,font-family,text-decoration,background-color
Example:
font-family: Arial,Helvetica,sans-serif;
font-size: 16px;
letter-spacing: 0em;
line-height: 1.6em;
font-weight: 400;
font-style: normal;
letter-spacing: .01em;
line-height: 1.8em;
text-transform: none;
color: rgba(23,23,23,.75);
text-decoration: underline;
Code Snippets Scenarios:
Q)how can you fetch the particular element color?
String c=driver.findElement(By.localtor("locatorvalue")).getCssValue("color");
Q)how can you find out particular element is udnerline or not?
String ud=driver.findElement(By.localtor("locatorvalue")).getCssValue("text-decoration");
4)getLocation():it fetches the element position in the webpage
Point p=driver.findElement(By.locator("locatorvalue")).getLocation();
SOP("x coordinate:"+p.getX()+"y cooardnate"+p.getY());
5)getSize():it fetches the height n width of the element
Dimension d=driver.findElement(By.locator("locatorvalue")).getSize();
SOP(d.getHeight()+" "+d.getWidth());
Object Location:
Now with Selenium 4, users can achieve the coordinates, dimension, height, width, etc. as the location of the web elements or object.
Please find the below code for your reference:
WebElement logo1=driver.Findelement(By.xpath(“//div[@id=’divLogo’]//img”));System.out.println(“Height:” +logo.getRect().getDimension().getHeight());System.out.println(“Height:” +logo.getRect().getDimension().getWidth());System.out.println(“X Location: “ +Logo.getRect().getX());System.out.println(“Y Location: “ +Logo.getRect().getY()); |
6)getTagName():it fetches the tag name for the element
String tag=driver.findElement(By.locator("locatorvalue")).getTagName();
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.