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

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

Source:SignInTest.java Github

copy

Full Screen

...42 public void signIn(String name,String se) {43 System.out.println((name+se));44 driver.get("http://localhost:8080/");45 driver.manage().window().setSize(new Dimension(1221, 825));46 driver.findElement(By.cssSelector(".login-button > .btn")).click();47 {48 WebElement element = driver.findElement(By.cssSelector(".login-button > .btn"));49 Actions builder = new Actions(driver);50 builder.moveToElement(element).perform();51 }52 {53 WebElement element = driver.findElement(By.tagName("body"));54 Actions builder = new Actions(driver);55 builder.moveToElement(element, 0, 0).perform();56 }57 {58 WebElement element = driver.findElement(By.cssSelector("#pane-user .el-form-item:nth-child(1) .el-input__inner"));59 Actions builder = new Actions(driver);60 builder.moveToElement(element).clickAndHold().perform();61 }62 {63 WebElement element = driver.findElement(By.cssSelector("#pane-user .el-form-item:nth-child(1) .el-input__inner"));64 Actions builder = new Actions(driver);65 builder.moveToElement(element).perform();66 }67 {68 WebElement element = driver.findElement(By.cssSelector("#pane-user .el-form-item:nth-child(1) .el-input__inner"));69 Actions builder = new Actions(driver);70 builder.moveToElement(element).release().perform();71 }72 driver.findElement(By.cssSelector("#pane-user .el-form-item:nth-child(1) .el-input__inner")).click();73 driver.findElement(By.cssSelector("#pane-user .el-form-item:nth-child(1) .el-input__inner")).sendKeys(name);74 driver.findElement(By.cssSelector("#pane-user .el-form-item:nth-child(2) .el-input__inner")).sendKeys(se);75 {76 WebElement element = driver.findElement(By.cssSelector(".el-button--primary"));77 Actions builder = new Actions(driver);78 builder.moveToElement(element).clickAndHold().perform();79 }80 {81 WebElement element = driver.findElement(By.cssSelector(".el-button--primary"));82 Actions builder = new Actions(driver);83 builder.moveToElement(element).perform();84 }85 {86 WebElement element = driver.findElement(By.cssSelector(".el-button--primary"));87 Actions builder = new Actions(driver);88 builder.moveToElement(element).release().perform();89 }90 if(se.equals(""))91 {92 assertThat(driver.findElement(By.cssSelector(".el-form-item:nth-child(2) .el-form-item__error")).getText(), is("请输入密码"));93 }94 if(name.equals(""))95 {96 assertThat(driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-form-item__error")).getText(), is("请输入用户名"));97 }98 if(name.equals("亚当122343"))99 {100 driver.findElement(By.cssSelector(".el-button--primary")).click();101 assertThat(driver.findElement(By.cssSelector(".el-message__content")).getText(), is("登录失败!用户不存在!"));102 }103 if(se.equals("1234"))104 {105 driver.findElement(By.cssSelector(".el-button--primary")).click();106 assertThat(driver.findElement(By.cssSelector("el-message__content")).getText(), is("登录失败!密码错误!"));107 }108 if(se.equals("123456")&&name.equals("亚当"))109 {110 driver.findElement(By.cssSelector(".el-button--primary")).click();111 assertThat(driver.findElement(By.cssSelector(".el-message")).getText(), is("登录成功"));112 }113 }114}...

Full Screen

Full Screen

Source:Case8Test.java Github

copy

Full Screen

...39 @Test40 public void case8() {41 driver.get("https://www.saucedemo.com./");42 driver.manage().window().setSize(new Dimension(1440, 809));43 driver.findElement(By.cssSelector("*[data-test=\"username\"]")).click();44 driver.findElement(By.cssSelector("*[data-test=\"username\"]")).sendKeys("standard_user");45 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).click();46 {47 WebElement element = driver.findElement(By.cssSelector("*[data-test=\"password\"]"));48 Actions builder = new Actions(driver);49 builder.doubleClick(element).perform();50 }51 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).sendKeys("secret_sauce");52 driver.findElement(By.cssSelector("*[data-test=\"login-button\"]")).click();53 {54 WebElement element = driver.findElement(By.cssSelector("*[data-test=\"login-button\"]"));55 Actions builder = new Actions(driver);56 builder.moveToElement(element).perform();57 }58 driver.findElement(By.cssSelector(".shopping_cart_badge")).click();59 driver.findElement(By.cssSelector("*[data-test=\"checkout\"]")).click();60 driver.findElement(By.cssSelector("*[data-test=\"continue\"]")).click();61 driver.findElement(By.cssSelector("*[data-test=\"firstName\"]")).click();62 driver.findElement(By.cssSelector("*[data-test=\"firstName\"]")).click();63 driver.findElement(By.cssSelector("*[data-test=\"firstName\"]")).sendKeys("Maria");64 driver.findElement(By.cssSelector(".checkout_info")).click();65 driver.findElement(By.cssSelector("*[data-test=\"lastName\"]")).click();66 driver.findElement(By.cssSelector("*[data-test=\"lastName\"]")).sendKeys("Gutierrez");67 driver.findElement(By.cssSelector(".checkout_info")).click();68 driver.findElement(By.cssSelector("*[data-test=\"postalCode\"]")).click();69 driver.findElement(By.cssSelector("*[data-test=\"postalCode\"]")).sendKeys("28003");70 driver.findElement(By.cssSelector("form")).click();71 driver.findElement(By.cssSelector("*[data-test=\"continue\"]")).click();72 {73 WebElement element = driver.findElement(By.cssSelector("*[data-test=\"continue\"]"));74 Actions builder = new Actions(driver);75 builder.moveToElement(element).perform();76 }77 }78}...

Full Screen

Full Screen

Source:MovieDBSelTestTest.java Github

copy

Full Screen

...41 @Test42 public void movieDBSelTest() {43 driver.get("http://54.235.49.14:3000/");44 driver.manage().window().maximize();45 driver.findElement(By.cssSelector(".addLangForm > input")).click();46 driver.findElement(By.cssSelector(".addLangForm > input")).sendKeys("TestLang");47 driver.findElement(By.cssSelector("button:nth-child(4)")).click();48// driver.findElement(By.cssSelector(".left:nth-child(7) tr:nth-child(9)")).click();49 driver.findElement(By.cssSelector("div:nth-child(1)")).click();50 driver.findElement(By.name("Language ID")).click();51 driver.findElement(By.name("Language ID")).sendKeys("12");52 driver.findElement(By.cssSelector("button:nth-child(2)")).click();53 driver.findElement(By.cssSelector("form:nth-child(11) > input")).click();54 driver.findElement(By.cssSelector("form:nth-child(11) > input")).sendKeys("AcaDemy Dinosaur ");55 driver.findElement(By.cssSelector("form:nth-child(1) > input:nth-child(1)")).click();56 driver.findElement(By.cssSelector("form:nth-child(1) > input:nth-child(1)")).sendKeys("1");57 driver.findElement(By.cssSelector("div:nth-child(9) input:nth-child(2)")).click();58 driver.findElement(By.cssSelector("div:nth-child(9) input:nth-child(2)")).sendKeys("GENERAL");59 driver.findElement(By.cssSelector("input:nth-child(3)")).sendKeys("KENOBI");60 driver.findElement(By.cssSelector("button:nth-child(5)")).click();61 driver.findElement(By.cssSelector("div:nth-child(1)")).click();62 driver.findElement(By.cssSelector("form:nth-child(11) > input")).click();63 driver.findElement(By.cssSelector("form:nth-child(11) > input")).sendKeys("FFF");64// driver.findElement(By.cssSelector(".left:nth-child(14) > tbody > tr:nth-child(1)")).click();65 driver.findElement(By.cssSelector("div:nth-child(1)")).click();66 }67}...

Full Screen

Full Screen

Source:Case9Test.java Github

copy

Full Screen

...39 @Test40 public void case9() {41 driver.get("https://www.saucedemo.com./");42 driver.manage().window().setSize(new Dimension(1440, 809));43 driver.findElement(By.cssSelector("*[data-test=\"username\"]")).click();44 driver.findElement(By.cssSelector("*[data-test=\"username\"]")).sendKeys("standard_user");45 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).click();46 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).sendKeys("secret_sauce");47 driver.findElement(By.cssSelector("*[data-test=\"login-button\"]")).click();48 {49 WebElement element = driver.findElement(By.cssSelector("*[data-test=\"login-button\"]"));50 Actions builder = new Actions(driver);51 builder.moveToElement(element).perform();52 }53 driver.findElement(By.linkText("2")).click();54 driver.findElement(By.cssSelector("*[data-test=\"checkout\"]")).click();55 driver.findElement(By.cssSelector("*[data-test=\"firstName\"]")).click();56 driver.findElement(By.cssSelector("*[data-test=\"firstName\"]")).sendKeys("María");57 driver.findElement(By.cssSelector("*[data-test=\"lastName\"]")).sendKeys("Gutiérrez Martín");58 driver.findElement(By.cssSelector("*[data-test=\"postalCode\"]")).sendKeys("28003");59 driver.findElement(By.cssSelector("*[data-test=\"continue\"]")).click();60 driver.findElement(By.cssSelector("*[data-test=\"finish\"]")).click();61 driver.findElement(By.cssSelector("*[data-test=\"back-to-products\"]")).click();62 driver.findElement(By.cssSelector(".shopping_cart_link")).click();63 }64}...

Full Screen

Full Screen

Source:Case5Test.java Github

copy

Full Screen

...39 @Test40 public void case5() {41 driver.get("https://www.saucedemo.com./");42 driver.manage().window().setSize(new Dimension(1440, 809));43 driver.findElement(By.cssSelector("*[data-test=\"username\"]")).click();44 driver.findElement(By.cssSelector("*[data-test=\"username\"]")).sendKeys("standard_user");45 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).click();46 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).click();47 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).sendKeys("secret_sauce");48 driver.findElement(By.cssSelector("*[data-test=\"login-button\"]")).click();49 {50 WebElement element = driver.findElement(By.cssSelector("*[data-test=\"login-button\"]"));51 Actions builder = new Actions(driver);52 builder.moveToElement(element).perform();53 }54 driver.findElement(By.cssSelector("*[data-test=\"remove-sauce-labs-backpack\"]")).click();55 driver.findElement(By.cssSelector("*[data-test=\"add-to-cart-sauce-labs-backpack\"]")).click();56 driver.findElement(By.linkText("1")).click();57 driver.findElement(By.cssSelector("*[data-test=\"continue-shopping\"]")).click();58 driver.findElement(By.cssSelector("*[data-test=\"add-to-cart-sauce-labs-bike-light\"]")).click();59 driver.findElement(By.linkText("2")).click();60 }61}...

Full Screen

Full Screen

Source:Case7Test.java Github

copy

Full Screen

...39 @Test40 public void case7() {41 driver.get("https://www.saucedemo.com./");42 driver.manage().window().setSize(new Dimension(1440, 809));43 driver.findElement(By.cssSelector("*[data-test=\"username\"]")).click();44 driver.findElement(By.cssSelector("*[data-test=\"username\"]")).sendKeys("standard_user");45 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).click();46 driver.findElement(By.cssSelector("*[data-test=\"password\"]")).sendKeys("secret_sauce");47 driver.findElement(By.cssSelector("*[data-test=\"login-button\"]")).click();48 driver.findElement(By.cssSelector("*[data-test=\"add-to-cart-sauce-labs-backpack\"]")).click();49 driver.findElement(By.linkText("1")).click();50 driver.findElement(By.cssSelector("*[data-test=\"continue-shopping\"]")).click();51 driver.findElement(By.cssSelector("*[data-test=\"add-to-cart-sauce-labs-bike-light\"]")).click();52 driver.findElement(By.cssSelector(".shopping_cart_badge")).click();53 driver.findElement(By.cssSelector("*[data-test=\"checkout\"]")).click();54 }55}...

Full Screen

Full Screen

Source:IEEETest.java Github

copy

Full Screen

...39 @Test40 public void iEEE() {41 driver.get("http://127.0.0.1:5500/fundamentos/calculadora.html");42 driver.manage().window().setSize(new Dimension(830, 1032));43 driver.findElement(By.cssSelector(".btnDigito:nth-child(20)")).click();44 driver.findElement(By.cssSelector(".btnDigito:nth-child(14)")).click();45 driver.findElement(By.cssSelector(".btnOperar:nth-child(5)")).click();46 driver.findElement(By.cssSelector(".btnDigito:nth-child(20)")).click();47 driver.findElement(By.cssSelector(".btnDigito:nth-child(15)")).click();48 driver.findElement(By.cssSelector(".btnOperar:nth-child(21)")).click();49 assertThat(driver.findElement(By.id("txtPantalla")).getText(), is("0,3"));50 }51}...

Full Screen

Full Screen

Source:DriverUtils.java Github

copy

Full Screen

...20 }21 public void GoPage(String link) {22 driver.get(link);23 }24 public WebElement waitForElementCssSelector(By cssSelector) {25 return driverWait.until(ExpectedConditions.visibilityOfElementLocated(cssSelector));26 }27 public List<WebElement> waitForElementsCssSelector(By cssSelector) {28 return driverWait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(cssSelector));29 }30 public WebElement elementCssSelector(By cssSelector) {31 return driver.findElement(cssSelector);32 }33 public List<WebElement> elementsCssSelector(By cssSelector) {34 return driver.findElements(cssSelector);35 }36 public void ScrollJSElement(WebElement element){37 JavascriptExecutor js = (JavascriptExecutor) driver;38 js.executeScript("arguments[0].scrollIntoView();", element);39 }40 public void moveToClickElement(WebElement element){41 Actions actions = new Actions(driver);42 actions.moveToElement(element).click().build().perform();43 }44 public void Quit() {45 driver.quit();46 }47}...

Full Screen

Full Screen

cssSelector

Using AI Code Generation

copy

Full Screen

1public class ByCssSelectorDemo {2 public static void main(String[] args) {3 WebDriver driver = new FirefoxDriver();4 WebElement searchBox = driver.findElement(By.cssSelector("input[name='q']"));5 searchBox.sendKeys("Selenium WebDriver");6 searchBox.submit();7 driver.quit();8 }9}10public class ByXpathDemo {11 public static void main(String[] args) {12 WebDriver driver = new FirefoxDriver();13 searchBox.sendKeys("Selenium WebDriver");14 searchBox.submit();15 driver.quit();16 }17}18public class ByLinkTextDemo {19 public static void main(String[] args) {20 WebDriver driver = new FirefoxDriver();21 WebElement searchBox = driver.findElement(By.linkText("Gmail"));22 searchBox.click();23 driver.quit();24 }25}26public class ByPartialLinkTextDemo {27 public static void main(String[] args) {28 WebDriver driver = new FirefoxDriver();29 WebElement searchBox = driver.findElement(By.partialLinkText("Gma"));30 searchBox.click();31 driver.quit();32 }33}34public class ByTagNameDemo {35 public static void main(String[] args) {36 WebDriver driver = new FirefoxDriver();37 WebElement searchBox = driver.findElement(By.tagName("input"));38 searchBox.sendKeys("Selenium WebDriver");39 searchBox.submit();40 driver.quit();41 }42}43public class ByNameDemo {44 public static void main(String[] args) {45 WebDriver driver = new FirefoxDriver();46 WebElement searchBox = driver.findElement(By.name("q"));47 searchBox.sendKeys("Selenium WebDriver");48 searchBox.submit();49 driver.quit();50 }51}52public class ByClassNameDemo {53 public static void main(String[]

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