How to use tagName method of org.openqa.selenium.By class

Best Selenium code snippet using org.openqa.selenium.By.tagName

Source:DefaultFieldDecoratorTest.java Github

copy

Full Screen

...52 @SuppressWarnings("unused") private List<WebElement> list1;53 @SuppressWarnings("unused") private List<Object> list2;54 @SuppressWarnings("unused") private Integer num;55 @SuppressWarnings("unused")56 @FindBy(tagName = "div")57 private List<WebElement> list3;58 @SuppressWarnings("unused")59 @FindBys({@FindBy(tagName = "div"), @FindBy(tagName = "a")})60 private List<WebElement> list4;61 @SuppressWarnings("unused")62 @FindAll({@FindBy(tagName = "div"), @FindBy(tagName = "a")})63 private List<WebElement> list5;64 @SuppressWarnings("unused")65 @FindBy(tagName = "div")66 private List<Object> list6;67 @SuppressWarnings("unused")68 @FindBys({@FindBy(tagName = "div"), @FindBy(tagName = "a")})69 private List<Object> list7;70 @SuppressWarnings("unused")71 @FindAll({@FindBy(tagName = "div"), @FindBy(tagName = "a")})72 private List<Object> list8;73 private FieldDecorator createDecoratorWithNullLocator() {74 return new DefaultFieldDecorator(field -> null);75 }76 private FieldDecorator createDecoratorWithDefaultLocator() {77 return new DefaultFieldDecorator(78 new DefaultElementLocatorFactory(null));79 }80 @Test81 public void decoratesWebElement() throws Exception {82 FieldDecorator decorator = createDecoratorWithDefaultLocator();83 assertThat(decorator.decorate(getClass().getClassLoader(),84 getClass().getDeclaredField("element1")),85 is(notNullValue()));...

Full Screen

Full Screen

Source:LinksAutomation.java Github

copy

Full Screen

...27 driver.get("https://www.ebay.com/");28 29 //Actions n = new Action(driver);30 System.out.println("Total number of links on page");31 System.out.println(driver.findElements(By.tagName("a")).size());32 // no of links in the footer33 System.out.println("Num of links in the footer");34WebElement footer= driver.findElement(By.xpath(".//footer[@id='glbfooter']"));35System.out.println(footer.findElements(By.tagName("a")).size());36WebElement header =driver.findElement(By.xpath(".//ul[@id='s0-container']"));37System.out.println("How many links are in the header section");38System.out.println(header.findElements(By.tagName("a")).size());39System.out.println("num of links in the buy part of the footer");40WebElement buy = driver.findElement(By.xpath(".//*[@id='gf-BIG']/table/tbody/tr/td[1]/ul"));41System.out.println(buy.findElements(By.tagName("a")).size());42String BeforeClicking = null;43String AfterClicking;44for(int i = 0; i<buy.findElements(By.tagName("a")).size();i++){45System.out.println(buy.findElements(By.tagName("a")).get(i).getText());46 47 if(buy.findElements(By.tagName("a")).get(i).getText().contains("Stores"))48 {49 System.out.println(driver.getTitle());50 BeforeClicking =driver.getTitle();51 52 //WebDriverWait dk = new WebDriverWait(driver,5);53 54 //driver.findElements(By.tagName("a"));55 56 // dk.until(ExpectedConditions.invisibilityOfElementLocated(By.tagName("a")));57 buy.findElements(By.tagName("a")).get(i).click();58 break;59 60 61 62 }63}64 System.out.println(driver.getTitle());6566 //next steps show you how to compare the get-title strings are they same or not67 /*AfterClicking = driver.getTitle();68 if(BeforeClicking!=AfterClicking){69 if(driver.getPageSource().contains("Find out what's inside"))70//boolean abc=driver.findElement(By.xpath(".//*[@id='CentralArea']/div/div[2]/table/tbody/tr[1]/td/div[1]/div/div")).isDisplayed(); 71//boolean ab = driver.findElement(By.cssSelector("div[id='CentralArea']")).isDisplayed(); ...

Full Screen

Full Screen

Source:WorkingWithwebElement.java Github

copy

Full Screen

...15 unTxtBx.sendKeys(Keys.CONTROL+"a" );16 Thread.sleep(2000);17 unTxtBx.sendKeys(Keys.DELETE);18 unTxtBx.sendKeys("xyz");19 String headerText = driver.findElement(By.tagName("h1")).getText();20 System.out.println(headerText); 21 WebElement pwdTxt = driver.findElement(By.name("pwd"));22 String value1=pwdTxt.getAttribute("placeholder");23 System.out.println(value1);24 pwdTxt.sendKeys("123456789abcd");25 String value2 = pwdTxt.getAttribute("value");26 System.out.println(value2);27 28 String cssvalue = driver.findElement(By.tagName("a")).getCssValue("font-size");29 System.out.println(cssvalue);30 31 Dimension dim = driver.findElement(By.tagName("img")).getSize();32 int height = dim.getHeight();33 int width =dim.getWidth();34 System.out.println("Height of Image" +height);35 System.out.println("Height of Image" +width);36 37 int siz = driver.findElement(By.tagName("body")).getSize().getHeight();38 System.out.println(siz);39 40 Point pt = driver.findElement(By.tagName("img")).getLocation();41 int x = pt.getX();42 int y= pt.getY();43 System.out.println("Image Location::: " + x +"\t" +y ); */44 45 WebElement chkBox = driver.findElement(By.className("MyCheckbox"));46 boolean flag = chkBox.isSelected();47 if (flag)48 {49 System.out.println("Checkbox is already enabled");50 }51 else52 {53 System.out.println("select checkbox");54 chkBox.click();...

Full Screen

Full Screen

Source:Exercise1.java Github

copy

Full Screen

...24 driver.get("https://www.ebay.com");25 Actions a= new Actions(driver);26 27 //number of links on a webpage28 int linksNumber = driver.findElements(By.tagName("a")).size();29 System.out.println("There are " + linksNumber + " links present on a page");30 31 //number of links in a footer32 WebElement footer = driver.findElement(By.xpath("//*[@id=\"hlGlobalFooter\"]"));33 int footerLinksNumber = footer.findElements(By.tagName("a")).size();34 System.out.println("There are " + footerLinksNumber + " links present in a footer");35 //number of links in second footer column36 WebElement column = driver.findElement(By.xpath("//*[@id=\"gf-BIG\"]/table/tbody/tr/td[2]/ul"));37 int columnLinksNumber = column.findElements(By.tagName("a")).size();38 System.out.println("There are " + columnLinksNumber + " links present in a second footer column");39 40 //printing link names from this section41 System.out.println("***********");42 String beforeClicking = null;43 String afterClicking;44 for(int i=0;i<columnLinksNumber; i++)45 {46 47 //System.out.println(column.findElements(By.tagName("a")).get(i).getText());48 if (column.findElements(By.tagName("a")).get(i).getText().contains("Site map"))49 {50 beforeClicking = driver.getTitle();51 System.out.println(beforeClicking);52 column.findElements(By.tagName("a")).get(i).click();53 System.out.println("Site Map page has been opened");54 break;55 }56 }57 58 afterClicking = driver.getTitle();59 System.out.println(afterClicking);60 if(beforeClicking != afterClicking)61 {62 //boolean abc = driver.findElement(By.xpath("/html/body/div[2]/div[1]/h1")).isDisplayed();63 if (driver.getPageSource().contains("sitemap"))64 {65 System.out.println("PASS");66 }...

Full Screen

Full Screen

Source:WebTableSample.java Github

copy

Full Screen

...25 /*26 //reading the tbody cell data27 List<WebElement> Rows = driver.findElements(By.xpath("//table/tbody/tr"));28 for(WebElement row : Rows){29 List<WebElement> cols = row.findElements(By.tagName("td"));30 for(WebElement cellData : cols){31 System.out.println(cellData.getText());32 if(cellData.getText().equals("QTP")){33 System.out.println("test case passed!");34 }35 }36 }*/37 List<WebElement> Rows = driver.findElements(By.xpath("//table/tbody/tr"));38 /*System.out.println(Rows.size()); //no of rows39 for(WebElement row : Rows){40 List<WebElement> cols = row.findElements(By.tagName("td"));41 for(WebElement cellData : cols){42 System.out.println(cellData.getText());43 if(cellData.getText().equals("Coded UI")){44 //System.out.println("test case passed!");45 cols.get(0).findElement(By.tagName("input")).click(); //checkbox click46 cols.get(4).findElement(By.tagName("input")).click(); //delete button click47 System.out.println(cols.get(4).findElement(By.tagName("input")).getAttribute("value"));48 }49 }50 }*/51 for(int i =0; i<Rows.size(); i++){52 List<WebElement> cols = Rows.get(i).findElements(By.tagName("td"));53 for(int j=1; j<=cols.size(); j++){54 String cell = driver.findElement(By.xpath("//table/tbody/tr["+(i+1)+"]/td[" +j+"]")).getText();55 System.out.println(cell);56 }57 }58 Thread.sleep(3000);59 driver.quit();60 }61}...

Full Screen

Full Screen

Source:Scope.java Github

copy

Full Screen

...27 /*28 * driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); WebDriverWait29 * w = new WebDriverWait(driver, 5);30 * w.until(ExpectedConditions.visibilityOfElementLocated(columndriver.31 * findElements(By.tagName("a"))));32 */33 34 //1.35 System.out.println(driver.findElements(By.tagName("a")).size());36 //2.37 WebElement footerdriver = driver.findElement(By.id("gf-BIG")); // limiting WebDriver scope38 System.out.println(footerdriver.findElements(By.tagName("a")).size());39 //3.40 WebElement columndriver = footerdriver.findElement(By.xpath("//table/tbody/tr/td[1]/ul"));41 System.out.println(columndriver.findElements(By.tagName("a")).size());42 //4.43 for(int i=1; i<columndriver.findElements(By.tagName("a")).size();i++) {44 45 46 String clickonlinktab = Keys.chord(Keys.CONTROL,Keys.ENTER);47 48 columndriver.findElements(By.tagName("a")).get(i).sendKeys(clickonlinktab);49 50 /*51 * columndriver.findElements(By.tagName("a")).get(i).click();52 * Thread.sleep(3000); driver.navigate().back();53 */54 55 Thread.sleep(3000L);56 57 }58 Set<String> abc = driver.getWindowHandles();59 Iterator<String> it = abc.iterator();60 61 while(it.hasNext()) {62 63 driver.switchTo().window(it.next());64 System.out.println(driver.getTitle());65 }...

Full Screen

Full Screen

Source:TestCase9.java Github

copy

Full Screen

...16 WebDriver driver = new ChromeDriver();17 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);18 Actions a = new Actions(driver);19 driver.get("https://www.rahulshettyacademy.com/AutomationPractice/");20 System.out.println(driver.findElements(By.tagName("a")).size());21 // count all the links only under the footer section22 WebElement footer = driver.findElement(By.id("gf-BIG")); // limiting the scope of driver23 System.out.println(footer.findElements(By.tagName("a")).size());24 // find the count in one of the footer coloumn25 WebElement footerColoumn = footer.findElement(By.xpath("//div[@id='gf-BIG']/table/tbody/tr/td[1]"));26 int size = footerColoumn.findElements(By.tagName("a")).size();27 System.out.println(size);28 // click on each link and checking if the pages are opening29 for (int i = 0; i < size; i++) {30 String KeyAction = Keys.chord(Keys.CONTROL, Keys.ENTER);31// a.moveToElement(footerColoumn.findElements(By.tagName("a")).get(i)).keyDown(Keys.CONTROL).click().build()32 // .perform();33 footerColoumn.findElements(By.tagName("a")).get(i).sendKeys(KeyAction);34 }35 driver.close();36 Set<String> url = driver.getWindowHandles();37 Iterator<String> it = url.iterator();38 /*39 * for (int i = 0; i < size; i++) { String pageId = it.next();40 * System.out.println(pageId); }41 */42 while (it.hasNext()) {43 driver.switchTo().window(it.next());44 System.out.println(driver.getTitle());45 driver.close();46 }47 }...

Full Screen

Full Screen

Source:CustomAnnotation.java Github

copy

Full Screen

...24 try {25 FindBy findBy = field.getAnnotation(FindBy.class);26 Preconditions.checkArgument(findBy != null, "Could not locate @FindBy annotation");27 String value = findBy.value();28 String tagName = findBy.tag();29 switch (tagName) {30 case "id":31 return new By.ById(value);32 case "xpath":33 return new By.ByXPath(value);34 case "css":35 return new By.ByCssSelector(value);36 case "className":37 return new By.ByClassName(value);38 case "linkText":39 return new By.ByLinkText(value);40 case "partialLinkText":41 return new By.ByPartialLinkText(value);42 case "name":43 return new By.ByName(value);44 case "tagName":45 return new By.ByTagName(value);46 }47 } catch (ElementNotVisibleException env) {48 // check if we have to throw exception from here to main test case49 env.toString();50 } catch (NoSuchElementException nse) {51 } catch (StaleElementReferenceException ser) {52 } catch (TimeoutException timeoutExc) {53 } catch (WebDriverException exc) {54 }55 return null;56 }57 @Override58 public boolean isLookupCached() {...

Full Screen

Full Screen

tagName

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 TagNameSelector {6 public static void main(String[] args) {7 System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Desktop\\chromedriver.exe");8 WebDriver driver = new ChromeDriver();9 WebElement email = driver.findElement(By.tagName("input"));10 email.sendKeys("

Full Screen

Full Screen

tagName

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.WebElement;5import java.util.List;6public class TagNameExample {7 public static void main(String[] args) {8 WebDriver driver = new ChromeDriver();9 List<WebElement> list = driver.findElements(By.tagName("a"));10 System.out.println("Total number of links: " + list.size());11 for (WebElement element : list) {12 System.out.println(element.getText());13 }14 driver.quit();15 }16}17driver.findElements(By.linkText("linkText"))18import org.openqa.selenium.By;19import org.openqa.selenium.WebDriver;20import org.openqa.selenium.chrome.ChromeDriver;21import org.openqa.selenium.WebElement;22import java.util.List;23public class LinkTextExample {24 public static void main(String[] args) {25 WebDriver driver = new ChromeDriver();26 List<WebElement> list = driver.findElements(By.linkText("Gmail"));27 System.out.println("Total number of links: " + list.size());28 for (WebElement element : list) {29 System.out.println(element.getText());30 }31 driver.quit();32 }33}

Full Screen

Full Screen

tagName

Using AI Code Generation

copy

Full Screen

1driver.findElement(By.tagName("input")).sendKeys("Hello World");2driver.findElement(By.tagName("input")).click();3driver.findElement(By.linkText("Google Search")).click();4driver.findElement(By.partialLinkText("Search")).click();5driver.findElement(By.name("q")).sendKeys("Hello World");6driver.findElement(By.name("q")).click();7driver.findElement(By.className("gNO89b")).click();8driver.findElement(By.cssSelector("input.gLFyf.gsfi")).sendKeys("Hello World");9driver.findElement(By.cssSelector("input.gLFyf.gsfi")).click();10driver.close();11Example 2: Using findElements() method of WebDriver interface12package com.javatpoint;13import org.openqa.selenium.By;14import org.openqa.selenium.WebDriver;15import org.openqa.selenium.chrome.ChromeDriver;16public class FindElementsExample {17 public static void main(String[] args) {18 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");19 WebDriver driver = new ChromeDriver();20 System.out.println(driver.findElements(By.tagName("a")).size());21 driver.close();22 }23}24Example 3: Using findElements() method of WebElement interface25package com.javatpoint;26import org.openqa.selenium.By;27import org.openqa.selenium.WebDriver;28import org.openqa.selenium.WebElement;29import org.openqa.selenium.chrome.ChromeDriver;30public class FindElementsExample2 {31 public static void main(String[] args) {32 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");33 WebDriver driver = new ChromeDriver();

Full Screen

Full Screen

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