How to use By.ByCssSelector class of org.openqa.selenium package

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

Source:BuyIphone13ProPage.java Github

copy

Full Screen

...7import java.time.Duration;8public class BuyIphone13ProPage {9 WebDriver driver;10 int timeout = 10;11 By iphoneMaxButton = new By.ByCssSelector("[data-autom='dimensionScreensize6_7inch']+label");12 By iphoneColor = new By.ByCssSelector("[data-autom='dimensionColorsierrablue']+label");13 By capacityButton = new By.ByCssSelector("[data-autom='dimensionCapacity128gb']+label");14 By tradeButton = new By.ByCssSelector("[data-autom='choose-noTradeIn']+label");15 By addToCartButton = new By.ByCssSelector(".button.button-block");16 By bande = new By.ByCssSelector(".as-standardnav-background");17 public BuyIphone13ProPage(WebDriver driver) {18 this.driver = driver;19 }20 public BuyIphone13ProPage selectIphone13ProMax() {21 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeout));22 wait.until(ExpectedConditions.visibilityOfElementLocated(bande));23 driver.findElement(iphoneMaxButton).click();24 wait.until(ExpectedConditions.visibilityOfElementLocated(iphoneColor));25 return this;26 }27 public BuyIphone13ProPage selectBlueAlpin() throws InterruptedException {28 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeout));29 wait.until(ExpectedConditions.elementToBeClickable(iphoneColor));30 driver.findElement(iphoneColor).click();...

Full Screen

Full Screen

Source:App.java Github

copy

Full Screen

...9import org.openqa.selenium.support.ui.WebDriverWait;10public class App {11 WebDriver driver ;12 String BASE_URL = "https://www.instagram.com/";13 By usernameLocator = new By.ByCssSelector("input[name='username']");14 By passwordLocator = new By.ByCssSelector("input[name='password']");15 By loginButtonLocator = new By.ByCssSelector("button[type='submit']");16 By instagramLogoLocator = By.className("s4Iyt");17 By infoLocator = By.className("g47SY");18 By postImageLocator = By.className("_9AhH0");19 By htmlTag = By.tagName("html");20 By likeButtonLocator = new By.ByCssSelector("span.fr66n");21 public App(){22 ChromeOptions options = new ChromeOptions();23 options.addArguments("start-maximized");24 WebDriverManager.chromedriver().setup();25 driver = new ChromeDriver(options);26 driver.get(BASE_URL);27 }28 public void loginWith(String username , String password){29 waitFor(usernameLocator);30 driver.findElement(usernameLocator).sendKeys(username);31 driver.findElement(passwordLocator).sendKeys(password);32 driver.findElement(loginButtonLocator).click();33 waitFor(instagramLogoLocator);34 }...

Full Screen

Full Screen

Source:Class2_Demoga.java Github

copy

Full Screen

...9 WebDriver driver = new ChromeDriver();10 driver.get("https://demoqa.com/text-box");11 driver.manage().window().maximize();12 driver.findElement(By.id("userName")).sendKeys("asdjasdskdj");13 WebElement nameText = driver.findElement(new By.ByCssSelector("input.mr-sm-2[placeholder=\"name@example.com\"]"));14 nameText.click();15 WebElement emailText = driver.findElement(new By.ByCssSelector("input.mr-sm-2[placeholder=\"name@example.com\"]"));16 emailText.sendKeys("yasinargln@gmail.com");17 WebElement adressText = driver.findElement(new By.ByCssSelector(".form-control[placeholder='Current Address']"));18 adressText.sendKeys("istanbul türkiye");19 WebElement currentAdressText = driver.findElement(new By.ByCssSelector(".form-control[placeholder='Current Address']"));20 currentAdressText.sendKeys("ASDSDSADMSADMSADSDMSD");21 WebElement permanentAdressText = driver.findElement(new By.ByCssSelector(".form-control[id='permanentAddress']"));22 permanentAdressText.sendKeys("BU ŞEKİLDE DE YAPILABİLİR");23 driver.findElement(By.xpath("//div/button[@id='submit']")).click();24 WebElement strnameText = driver.findElement(By.xpath("//div/p[@id='name']"));25 String name = strnameText.getText();26 System.out.println(name);27 WebElement strEmailText = driver.findElement(By.xpath("//div/p[@id='email']"));28 String email = strEmailText.getText();29 System.out.println(email);30 }31}...

Full Screen

Full Screen

Source:RozetkaProductsListPage.java Github

copy

Full Screen

...6import java.util.List;7public class RozetkaProductsListPage {8 private final WebDriver driver = SeleniumConfig.getInstance().getWebDriver();9 public WebElement getSmallTilesButton() {10 return driver.findElement(new By.ByCssSelector("[title='Мала плитка']"));11 }12 public WebElement getLargeTilesButton() {13 return driver.findElement(new By.ByCssSelector("[title='Крупна плитка']"));14 }15 public WebElement getFirstCatalogItem() {16 return driver.findElement(new By.ByCssSelector(".catalog-grid__cell"));17 }18 public WebElement getFirstCatalogItemBuyButton() {19 return driver.findElement(new By.ByCssSelector(".goods-tile .buy-button"));20 }21 public WebElement getFirstCatalogItemCompareButton() {22 return driver.findElement(new By.ByCssSelector(".goods-tile .compare-button"));23 }24 public WebElement getCartCounter() {25 return driver.findElement(new By.ByCssSelector("button[opencart] span.counter"));26 }27 public WebElement getComparisonListCounter() {28 return driver.findElement(new By.ByCssSelector("rz-comparison span.counter"));29 }30 public List<WebElement> getProductsList() {31 return driver.findElements(new By.ByCssSelector("ul.catalog-grid li"));32 }33 public WebElement getShowMoreButton() {34 return driver.findElement(new By.ByCssSelector("a.show-more"));35 }36}...

Full Screen

Full Screen

Source:CartPage.java Github

copy

Full Screen

...3import org.openqa.selenium.WebElement;4import java.util.List;5import org.openqa.selenium.By;6public class CartPage extends BasePage {7 By productNameLocator = new By.ByCssSelector(".shoppingcart-item");8 By productPriceLocator = new By.ByCssSelector("div.shoppingcart-item span.rd-cart-item-price");9 By productPlusLocator = new By.ByCssSelector("a.plus");10 By productQuantityLocator = new By.ByCssSelector("input.item-quantity-input");11 By productDeleteLocator = new By.ByCssSelector("a.cart-square-link[title=Sil]");12 By productScDeleteLocator = new By.ByCssSelector(".sc-delete");13 By emptyBasketLocator = new By.ByCssSelector(".cart-empty-title");14 public CartPage(WebDriver driver) {15 super(driver);16 }17 public Boolean checkIfProductAdded() {18 return getProducts().size() > 0;19 }20 private List<WebElement> getProducts() {21 return findAll(productNameLocator);22 }23 public String getProductPrice() {24 return find(productPriceLocator).getText();25 }26 public void incrementCount() {27 click(productPlusLocator);...

Full Screen

Full Screen

Source:Cs_Test2.java Github

copy

Full Screen

...7 @Test8 public void forgotPasswordTest() {9 driver.get("https://www.ciceksepeti.com/uye-girisi");10 driver.manage().window().maximize();11 driver.findElement(new By.ByCssSelector("a[class='login__forgot-password js-forgot-password']")).click();12 WebElement mail = driver.findElement(By.id("Mail"));13 mail.click();14 mail.sendKeys("uzundagdeniz@gmail.com");15 driver.findElement(new By.ByCssSelector("button[class='btn btn-lg btn-primary form-password-recovery__btn js-password-recovery-button']")).click();16 }17 @Test18 public void OrderTrac() {19 driver.get("https://www.ciceksepeti.com/siparis-takip");20 driver.findElement(new By.ByCssSelector("input[name='OrderCode']")).click();21 driver.findElement(new By.ByCssSelector("input[name='OrderCode']")).sendKeys("123456");22 driver.findElement(new By.ByCssSelector("input[name='CustomerEmail']")).click();23 driver.findElement(new By.ByCssSelector("input[name='CustomerEmail']")).sendKeys("uzundagdeniz@gmail.com");24 driver.findElement(new By.ByCssSelector("button[class='order-tracking-form__btn btn btn-primary btn-lg js-order-tracking-button']")).click();25 }26}...

Full Screen

Full Screen

Source:OnurMuhSeleniumTestApplicationTests.java Github

copy

Full Screen

...20 driver = new ChromeDriver();21 driver.get(UtilsTest.WEBPAGE_URL);22 23 WebDriverWait wait = new WebDriverWait(driver, 5);24 wait.until(ExpectedConditions.elementToBeClickable(new By.ByCssSelector("a[data-purpose='header-login']")));25 26 WebElement searchInput = driver.findElement(new By.ByCssSelector("input[id*='search-form-autocomplete--3']"));27 searchInput.click();28 searchInput.sendKeys(this.searchText);29 30 WebElement searchButton = driver.findElement(new By.ByCssSelector("div.header--search-bar--1_mS0 button"));31 searchButton.click();32 33 34 }35}...

Full Screen

Full Screen

Source:StorePage.java Github

copy

Full Screen

...8import org.openqa.selenium.support.ui.Wait;9public class StorePage extends BasePage {10 11 12 By.ByCssSelector byStoreNavBarLink = new By.ByCssSelector("ul.navbar-nav > li > a[href='/store']");13 By.ByXPath byDelayedTextBox = new By.ByXPath("//app-root/div[2]/tienda/div[1]/div[4]/input[1]");14 By.ByCssSelector byDelayedButton = new By.ByCssSelector("parque button");15 16 int interval = 0;17 int timeElapsed = 0;18 19 public StorePage() {20 21 }22 public void sendKeyToDelayedTextBoxAndReturnWhenButtonAppears() {23 24 }25 26 public void clickOnStoresNavBarLink() {27 28 WebElement storeNavBarLink = driver.findElement(byStoreNavBarLink);...

Full Screen

Full Screen

By.ByCssSelector

Using AI Code Generation

copy

Full Screen

1By byCssSelector = By.ByCssSelector("input[name='q']");2By byId = By.ById("lst-ib");3By byLinkText = By.ByLinkText("Gmail");4By byPartialLinkText = By.ByPartialLinkText("Gma");5By byClassName = By.ByClassName("gb_P");6By byTagName = By.ByTagName("a");7By byCssSelector = By.ByCssSelector("input[name='q']");8By byId = By.ById("lst-ib");9By byLinkText = By.ByLinkText("Gmail");10By byPartialLinkText = By.ByPartialLinkText("Gma");11By byClassName = By.ByClassName("gb_P");12By byTagName = By.ByTagName("a");13By byCssSelector = By.ByCssSelector("input[name='q']");14By byId = By.ById("lst-ib");15By byLinkText = By.ByLinkText("Gmail");

Full Screen

Full Screen

By.ByCssSelector

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;5import org.openqa.selenium.support.ui.Select;6public class ByCssSelector {7 public static void main(String[] args) {8 WebDriver driver = new ChromeDriver();9 driver.manage().window().maximize();10 driver.switchTo().frame("iframeResult");11 WebElement element = driver.findElement(By.cssSelector("select[name='cars']"));12 Select select = new Select(element);13 select.selectByVisibleText("Volvo");14 driver.quit();15 }16}17import org.openqa.selenium.By;18import org.openqa.selenium.WebDriver;19import org.openqa.selenium.WebElement;20import org.openqa.selenium.chrome.ChromeDriver;21import org.openqa.selenium.support.ui.Select;22public class ByXPath {23 public static void main(String[] args) {24 WebDriver driver = new ChromeDriver();25 driver.manage().window().maximize();26 driver.switchTo().frame("iframeResult");27 Select select = new Select(element);28 select.selectByVisibleText("Volvo");29 driver.quit();30 }31}

Full Screen

Full Screen

By.ByCssSelector

Using AI Code Generation

copy

Full Screen

1var driver = new FirefoxDriver();2var searchBox = driver.findElement(By.ByCssSelector(".gsfi"));3searchBox.sendKeys("Selenium WebDriver");4searchBox.submit();5driver.quit();6var driver = new FirefoxDriver();7searchBox.sendKeys("Selenium WebDriver");8searchBox.submit();9driver.quit();10var driver = new FirefoxDriver();11var searchBox = driver.findElement(By.ByClassName("gsfi"));12searchBox.sendKeys("Selenium WebDriver");13searchBox.submit();14driver.quit();15var driver = new FirefoxDriver();16var searchBox = driver.findElement(By.ById("lst-ib"));17searchBox.sendKeys("Selenium WebDriver");18searchBox.submit();19driver.quit();20var driver = new FirefoxDriver();21var searchBox = driver.findElement(By.ByName("q"));22searchBox.sendKeys("Selenium WebDriver");23searchBox.submit();24driver.quit();25var driver = new FirefoxDriver();26var searchBox = driver.findElement(By.ByLinkText("Gmail"));27searchBox.click();28driver.quit();29var driver = new FirefoxDriver();30var searchBox = driver.findElement(By.ByPartialLinkText("Gmai"));31searchBox.click();32driver.quit();33var driver = new FirefoxDriver();34var searchBox = driver.findElement(By.ByTagName("input"));35searchBox.sendKeys("Selenium WebDriver");36searchBox.submit();37driver.quit();38var driver = new FirefoxDriver();39var searchBox = driver.findElement(By.ByClassName("gsfi"));40searchBox.sendKeys("Selenium WebDriver");

Full Screen

Full Screen

By.ByCssSelector

Using AI Code Generation

copy

Full Screen

1package com.selenium;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6public class ByCssSelector {7 public static void main(String[] args) {8 System.setProperty("webdriver.chrome.driver", "C:\\Users\\DELL\\Downloads\\chromedriver.exe");9 WebDriver driver = new ChromeDriver();10 WebElement element = driver.findElement(By.cssSelector("input[name='q']"));11 element.sendKeys("Selenium");12 element.submit();13 driver.close();14 driver.quit();15 }16}17driver.findElement(By.className("class name"));18package com.selenium;19import org.openqa.selenium.By;20import org.openqa.selenium.WebDriver;21import org.openqa.selenium.WebElement;22import org.openqa.selenium.chrome.ChromeDriver;23public class ByClassName {24 public static void main(String[] args) {25 System.setProperty("webdriver.chrome.driver", "C:\\Users\\DELL\\Downloads\\chromedriver.exe");26 WebDriver driver = new ChromeDriver();27 WebElement element = driver.findElement(By.className("gLFyf"));28 element.sendKeys("Selenium");29 element.submit();30 driver.close();31 driver.quit();32 }33}34driver.findElement(By.linkText("link text"));35package com.selenium;36import org.openqa.selenium.By;37import org.openqa.selenium.WebDriver;38import org.openqa.selenium.WebElement;39import org.openqa.selenium.chrome.ChromeDriver;40public class ByLinkText {41 public static void main(String[] args) {42 System.setProperty("webdriver.chrome.driver", "C:\\Users\\DELL\\Downloads\\chromedriver.exe");43 WebDriver driver = new ChromeDriver();44 WebElement element = driver.findElement(By.linkText("Gmail"));45 element.click();46 driver.close();47 driver.quit();48 }49}50driver.findElement(By.partialLinkText("partial link

Full Screen

Full Screen

By.ByCssSelector

Using AI Code Generation

copy

Full Screen

1By by = By.ByCssSelector("input[name='q']");2By by = By.ById("lst-ib");3By by = By.ByName("q");4By by = By.ByClassName("gsfi");5By by = By.ByTagName("input");6By by = By.ByLinkText("Gmail");7By by = By.ByPartialLinkText("Mail");8By by = By.ByCssSelector("input[name='q']");9By by = By.ByClassName("gsfi");10By by = By.ByTagName("input");

Full Screen

Full Screen
copy
1public static String gobble(InputStream in, String charsetName) throws IOException {2 ByteArrayOutputStream bos = new ByteArrayOutputStream();3 in.transferTo(bos);4 return bos.toString(charsetName);5}6
Full Screen
copy
1private String readStream(InputStream iStream) throws IOException {2 //build a Stream Reader, it can read char by char3 InputStreamReader iStreamReader = new InputStreamReader(iStream);4 //build a buffered Reader, so that i can read whole line at once5 BufferedReader bReader = new BufferedReader(iStreamReader);6 String line = null;7 StringBuilder builder = new StringBuilder();8 while((line = bReader.readLine()) != null) { //Read till end9 builder.append(line);10 builder.append("\n"); // append new line to preserve lines11 }12 bReader.close(); //close all opened stuff13 iStreamReader.close();14 //iStream.close(); //EDIT: Let the creator of the stream close it!15 // some readers may auto close the inner stream16 return builder.toString();17}18
Full Screen
copy
1private static final int BUFFER_SIZE = 4 * 1024;23public static String inputStreamToString(InputStream inputStream, String charsetName)4 throws IOException {5 StringBuilder builder = new StringBuilder();6 InputStreamReader reader = new InputStreamReader(inputStream, charsetName);7 char[] buffer = new char[BUFFER_SIZE];8 int length;9 while ((length = reader.read(buffer)) != -1) {10 builder.append(buffer, 0, length);11 }12 return builder.toString();13}14
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.

Most used methods in By.ByCssSelector

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful