How to use getAttribute method of org.openqa.selenium.Interface WebElement class

Best Selenium code snippet using org.openqa.selenium.Interface WebElement.getAttribute

copy

Full Screen

...87 {88 return by.findElements(this);89 }90 @Override91 public String getAttribute(String attr)92 {93 String value = null;94 if ("index".equals(attr))95 {96 IHTMLOptionElement option = watijElem.queryInterface(IHTMLOptionElement.class);97 value = Integer.toString(option.index());98 }99 else if ("text".equals(attr))100 {101 value = watijElem.innerText();102 }103 else if ("value".equals(attr))104 {105 if ("OPTION".equalsIgnoreCase(watijElem.tagName()))106 {107 value = watijElem.queryInterface(IHTMLOptionElement.class).value();108 }109 else if ("INPUT".equalsIgnoreCase(watijElem.tagName()))110 {111 watijElem.queryInterface(IHTMLInputElement.class).value();112 }113 }114 else115 {116 value = watijElem.getAttribute(attr, RETURN_AS_BSTR).toString();117 }118 return value;119 }120 @Override121 public String getCssValue(String cssAttr)122 {123 return watijElem.style().getAttribute(cssAttr, RETURN_AS_BSTR).toString();124 }125 @Override126 public Point getLocation()127 {128 IHTMLElement2 elem2 = watijElem.queryInterface(IHTMLElement2.class);129 IHTMLRect bounds = elem2.getBoundingClientRect();130 return new Point(bounds.left(), bounds.top());131 }132 @Override133 public Dimension getSize()134 {135 IHTMLElement2 elem2 = watijElem.queryInterface(IHTMLElement2.class);136 IHTMLRect bounds = elem2.getBoundingClientRect();137 return new Dimension(bounds.right() - bounds.left(), bounds.bottom() - bounds.top());...

Full Screen

Full Screen
copy

Full Screen

...88 } catch (NoSuchElementException e) {89 String optionList = "";90 List<WebElement> optionsList = innerSelect.getOptions();91 for (WebElement option : optionsList) {92 optionList += option.getAttribute("value") + " | ";93 }94 TestReporter95 .interfaceLog(" The value of <b>[ " + value + "</​b> ] was not found in Listbox [ <b>"96 + getElementLocatorInfo() + " </​b>]. Acceptable values are " + optionList + " ]");97 TestReporter.logTrace("Exiting ListboxImpl#selectValue");98 throw new OptionNotInListboxException("The value of [ " + value + " ] was not found in Listbox [ "99 + getElementLocatorInfo() + " ]. Acceptable values are " + optionList, getWrappedDriver());100 }101 } else {102 TestReporter.interfaceLog("Skipping input to Textbox [ <b>" + getElementLocatorInfo() + " </​b> ]");103 }104 TestReporter.logTrace("Exiting ListboxImpl#selectValue");105 }106 /​**...

Full Screen

Full Screen
copy

Full Screen

...48 assertTrue(txtNome.isEnabled());49 50 txtNome.sendKeys("Antônio");51 52 assertEquals("Antônio", txtNome.getAttribute("value"));53 54 }55 56 @Test57 @Category(NegativeInterface.class)58 public void testValidaEnable() {59 WebElement txtNome = driver.findElement(By.name("txtbox1"));60 WebElement txtDisable = driver.findElement(By.name("txtbox2"));61 62 assertTrue(txtNome.isEnabled());63 assertFalse(txtDisable.isEnabled());64 }65 66 @Test67 @Category(NegativeInterface.class)68 public void testValidaRadioButton() {69 70 List<WebElement> listRadios = driver.findElements(By.name("radioGroup1"));71 72 for(WebElement radio: listRadios) {73 if (radio.getAttribute("value").equals("Radio Button 3 selecionado")){74 radio.click();75 }76 }77 78 assertFalse(listRadios.get(1).isSelected());79 assertTrue(listRadios.get(2).isSelected());80 }81 82 @Test83 @Category(NegativeInterface.class)84 public void testValidaCheckBox() throws InterruptedException {85 List<WebElement> listCheckBox = driver.findElements(By.name("chkbox"));86 87 for (WebElement check: listCheckBox) {88 if ((check.getAttribute("value").equals("Check Box 2 selecionado")) ||89 (check.getAttribute("value").equals("Check Box 3 selecionado"))){90 check.click();91 }92 }93 94 assertTrue(listCheckBox.get(1).isSelected());95 assertTrue(listCheckBox.get(2).isSelected());96 assertFalse(listCheckBox.get(3).isSelected());97 }98 99 @Test100 @Category(PositiveInterface.class)101 public void testDropDownSingle() throws InterruptedException {102 WebElement dropDownSingle = driver.findElement(By.name("dropdownlist"));103 104 Select selectSingle = new Select(dropDownSingle);105 106 selectSingle.selectByVisibleText("Item 1");107 selectSingle.selectByVisibleText("Item 2");108 selectSingle.selectByVisibleText("Item 7");109 110 assertEquals("Item 7", selectSingle.getFirstSelectedOption().getText());111 }112 113 @Test114 @Category(PositiveInterface.class)115 public void testDropDownMulti() {116 WebElement dropDownMulti = driver.findElement(By.name("multiselectdropdown"));117 118 Select selectMulti = new Select(dropDownMulti);119 120 selectMulti.selectByIndex(4); 121 selectMulti.selectByVisibleText("Item 9");122 selectMulti.selectByIndex(7);123 124 List<WebElement> allSelected = selectMulti.getAllSelectedOptions();125 126 assertEquals(3, allSelected.size());127 128 assertEquals("Item 5", allSelected.get(0).getText());129 assertEquals("Item 8", allSelected.get(1).getText());130 assertEquals("Item 9", allSelected.get(2).getText());131 132 selectMulti.deselectByIndex(7);133 134 allSelected = selectMulti.getAllSelectedOptions();135 136 assertEquals(2, allSelected.size());137 assertEquals("Item 5", allSelected.get(0).getText());138 assertEquals("Item 9", allSelected.get(1).getText()); 139 }140 141 @Ignore("Bug dectado e cadastrado no Jira BUG-123456")142 @Test143 @Category(PositiveInterface.class)144 public void testiFrames() throws InterruptedException {145 146 driver.switchTo().frame("iframe_b");147 148 WebElement btnAceitarCookies = driver.findElement(By.cssSelector("a.cc-btn.cc-ALLOW"));149 btnAceitarCookies.click();150 151 WebElement txtSearch = driver.findElement(By.id("s")); 152 txtSearch.sendKeys("Antonio");153 assertEquals("Antonio", txtSearch.getAttribute("value"));154 driver.switchTo().defaultContent();155 156 Thread.sleep(3000);157 158 driver.switchTo().frame("iframe_d");159 160 Thread.sleep(3000);161 162 WebElement menu = driver.findElement(By.id("dropdownButton"));163 menu.click();164 165 WebElement txtSearchWeb = driver.findElement(By.name("search"));166 txtSearchWeb.sendKeys("Antonio");167 assertEquals("Antonio", txtSearchWeb.getAttribute("value"));168 }169 170 @Test171 @Category(PositiveInterface.class)172 public void testAlert() {173 WebElement btnAlert = driver.findElement(By.name("alertbtn"));174 btnAlert.click();175 176 Alert alert = driver.switchTo().alert();177 assertEquals("Eu sou um alerta!", alert.getText());178 alert.dismiss();179 }180}...

Full Screen

Full Screen
copy

Full Screen

...46 public String getTagName() {47 return _delegate.getTagName();48 }49 @Override50 public String getAttribute(String name) {51 return _delegate.getAttribute(name);52 }53 @Override54 public boolean isSelected() {55 return _delegate.isSelected();56 }57 @Override58 public boolean isEnabled() {59 return _delegate.isEnabled();60 }61 @Override62 public String getText() {63 return _delegate.getText();64 }65 @Override...

Full Screen

Full Screen
copy

Full Screen

...41 Reporter.log("Clicked on: " + element.getText());42 }43 }44 } catch (Exception e) {45 elementText = element.getAttribute("textcontent");46 try {47 if (!elementText.isEmpty()) {48 if (elementText.length() < 100) {49 System.out.println("Clicked on: " + element.getAttribute("textcontent"));50 Reporter.log("Clicked on: " + element.getAttribute("textcontent"));51 }52 }53 } catch (Exception ex) {54 System.out.println("Clicked on: " + element.toString());55 Reporter.log("Clicked on: " + element.toString());56 }57 }58 }59 /​* (non-Javadoc)60 * @see org.openqa.selenium.support.events.AbstractWebDriverEventListener#afterClickOn(org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)61 */​62 public void afterClickOn(WebElement element, WebDriver driver) {63 }64 /​* (non-Javadoc)...

Full Screen

Full Screen
copy

Full Screen

...29 System.out.println("Button Name: "+signupBtn.getText());30 31 /​**32 * <button type="submit" class="btn btn-primary" name="signup"> Submit </​button>33 * getAttribute("attribute_name")- first identify the webelement dn we can user getAttribute("",) 34 * to get the html attribute value based on attribute name35 */​36 System.out.println("name attribute value: "+signupBtn.getAttribute("name"));37 System.out.println("class attribute value: "+signupBtn.getAttribute("class"));38 System.out.println("type attribute value: "+signupBtn.getAttribute("type"));39 40 /​**41 * findElements(By): to identify multiple elements from the web page and returns List<WebElement>42 * if you want to count the identified element dn use size()43 * if you want to get specific element from the list use get(index) by using passing the required element44 * index number or use for loop to iterate all the list elements45 */​46 List<WebElement> buttons=driver.findElements(By.tagName("button"));47 System.out.println("Button count: "+buttons.size());48 for(int i=0;i<buttons.size();i++) {49 System.out.println(buttons.get(i).getAttribute("class"));50 }51 }52}...

Full Screen

Full Screen
copy

Full Screen

...13 WebElement getElementPresenceWithTimeout(By by, int timeout);14 WebElement getElementByText ( String text );15 List<WebElement> getElementsByText (String text );16 List<WebElement> getElementsByTag ( String tagName );17 Optional<String> getAttribute (WebElement elem, String attributeName );18 Optional<String> getAttribute ( By by, String attributeName );19 void clickElement(By by);20 void clickElement(WebElement element);21 void clickElementJS ( By by );22 void clickElementJS (WebElement elem );23}...

Full Screen

Full Screen
copy

Full Screen

...22 WebElement name =driver.findElement(By.id("email"));23 24 name.sendKeys("simanchal");25 26 String st= name.getAttribute("value");27 28 29 WebElement pwd =driver.findElement(By.id("pass"));30 31 pwd.sendKeys(st);32 33 System.out.println(pwd.getAttribute("value"));34 35 36 37 38 3940 }4142} ...

Full Screen

Full Screen

getAttribute

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5public class GetAttribute {6public static void main(String[] args) {7WebDriver driver = new ChromeDriver();8driver.manage().window().maximize();9driver.switchTo().frame("iframeResult");10System.out.println("Attribute value is: "+text);11driver.quit();12}13}

Full Screen

Full Screen

getAttribute

Using AI Code Generation

copy

Full Screen

1driver.findElement(By.id("link1")).getAttribute("href");2driver.findElement(By.id("link1")).getCssValue("color");3driver.findElement(By.id("link1")).getTagName();4driver.findElement(By.id("link1")).getText();5driver.findElement(By.id("link1")).getSize();6driver.findElement(By.id("link1")).getLocation();7driver.findElement(By.id("link1")).isEnabled();8driver.findElement(By.id("link1")).isDisplayed();9driver.findElement(By.id("link1")).isSelected();10driver.findElement(By.id("link1")).clear();11driver.findElement(By.id

Full Screen

Full Screen

getAttribute

Using AI Code Generation

copy

Full Screen

1package com.selenium4beginners.java.webdriver;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6public class GetAttributeMethod {7 public static void main(String[] args) {8 System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");9 WebDriver driver = new ChromeDriver();10 WebElement linkElement = driver.findElement(By.linkText("This is a Ajax link"));11 String linkURL = linkElement.getAttribute("href");12 System.out.println(linkURL);13 driver.close();14 }15}

Full Screen

Full Screen

StackOverFlow community discussions

Questions
Discussion

How to open a link in new tab (chrome) using Selenium WebDriver?

getText() method of selenium chrome driver sometimes returns an empty string

How to test if a WebElement Attribute exists using Selenium Webdriver

What is the fix for class file for com.google.common.base.predicate not found?

Selenium abotring with &quot;UnreachableBrowserException/Address already in use&quot; occurring on one computer

How can I extend the Selenium By.class to create more flexibility?

Unable to create new remote session. Appium

Selenium Web Driver : Handle Confirm Box using Java

What is JavaScriptExecutor in Selenium?

Page scroll up or down in Selenium WebDriver (Selenium 2) using java

this below code works for me in Selenium 3 and chrome version 58.

    WebDriver driver = new ChromeDriver();
    driver.get("http://yahoo.com");  
    ((JavascriptExecutor)driver).executeScript("window.open()");
    ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
    driver.switchTo().window(tabs.get(1));
    driver.get("http://google.com");
https://stackoverflow.com/questions/34829329/how-to-open-a-link-in-new-tab-chrome-using-selenium-webdriver

Blogs

Check out the latest blogs from LambdaTest on this topic:

Top 10 Books for Getting Started with Automation Testing

Are you looking for the top books for Automation Testers? Ah! That’s why you are here. When I hear the term book, This famous saying always spins up in my head.

Looking Back At 2018 Through Our Best 18 Cross Browser Testing Blogs

Throwbacks always bring back the best memories and today’s blog is all about throwbacks of the best cross browser testing blogs written at LambdaTest in 2018. It is the sheer love and thirst for knowledge of you, our readers who have made these logs the most liked and read blogs in 2018.

Selenium Grid Setup Tutorial For Cross Browser Testing

When performing cross browser testing manually, one roadblock that you might have hit during the verification phase is testing the functionalities of your web application/web product across different operating systems/devices/browsers are the test coverage with respect to time. With thousands of browsers available in the market, automation testing for validating cross browser compatibility has become a necessity.

Selenium with Python Tutorial: Running First PyUnit Script

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Python Tutorial.

Common Mistakes Made By Web Developers And How To Avoid Them

Ever-since the introduction of World Wide Web in 1990, the domain of web development has evolved dynamically from web pages to web applications. End users no longer browse web pages for reading static content. Websites now have dynamic features to increase their engagement rate. Interactive websites are being developed using which users can perform their day to day activities like shopping for groceries, banking, paying taxes, etc. However, these applications are developed by human beings, and mistakes are supposed to happen. Often a simple mistake can impact a critical functionality in your website that will lead the user to move away to a different website, reducing your profit and SERP ranking. In this article, we shall discuss the common mistakes made by developers while developing a web application.

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful