Automation QA Testing Course Content

WebElement Return Type Methods in Selenium WebDriver

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());

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.