How to use InvalidSelectorException class of org.openqa.selenium package

Best Selenium code snippet using org.openqa.selenium.InvalidSelectorException

InvalidSelectorException org.openqa.selenium.InvalidSelectorException

The WebDriver error - invalid selector error, happens when an element fetch is tried with an unknown web element selection method.

The allowed selection methods are with partial link text, link text, CSS, XPath and tag name. If any other unknown selector strategy is used, it is rejected by throwing this error.

Example

Lets assume the element is

copy
1<a class="main-item" href="#">Personal Loans</a>

and script tried to access it with below xpath

copy
1//*[@id='main-nav']/ul/li[1]/a'] 2 3driver.findElement(By.xpath("//*[@id='main-nav']/ul/li[1]/a[1]']"))

then it throws

org.openqa.selenium.InvalidSelectorException: invalid selector

Solutions:

  • verify locator
  • check for locator syntex
  • try to access the element with different locator

Code Snippets

Here are code snippets that can help you understand more how developers are using

Source:MethodCalledOnEntityWithInvalidLocatorFailsOnTest.java Github

copy

Full Screen

...6import integration.IntegrationTest;7import org.junit.jupiter.api.BeforeEach;8import org.junit.jupiter.api.Disabled;9import org.junit.jupiter.api.Test;10import org.openqa.selenium.InvalidSelectorException;11import static com.codeborne.selenide.CollectionCondition.exactTexts;12import static com.codeborne.selenide.Condition.*;13import static com.codeborne.selenide.Selenide.$;14import static com.codeborne.selenide.Selenide.$$;15@Disabled16class MethodCalledOnEntityWithInvalidLocatorFailsOnTest extends IntegrationTest {17 @BeforeEach18 void openPage() {19 givenHtml(20 "<ul>Hello to:",21 "<li class='the-expanse detective'>Miller <label>detective</label></li>",22 "<li class='the-expanse missing'>Julie Mao</li>",23 "</ul>"24 );25 Configuration.timeout = 0;26 }27 @Test28 void shouldCondition_When$Element_WithInvalidLocator() {29 SelenideElement element = $("##invalid-locator");30 try {31 element.shouldHave(text("Miller"));32 fail("Expected ElementNotFound");33 } catch (ElementNotFound expected) {34 assertThat(expected)35 .hasMessageStartingWith("Element not found {##invalid-locator}");36 assertThat(expected.getScreenshot())37 .contains(Configuration.reportsFolder);38 assertThat(expected.getCause())39 .isInstanceOf(InvalidSelectorException.class);40 assertThat(expected.getCause())41 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +42 "The following error occurred:\n" +43 "InvalidSelectorError: An invalid or illegal selector was specified");44 }45 //todo - need to fix46 /*47 org.openqa.selenium.InvalidSelectorException:48 The given selector ##invalid-locator is either invalid or does not result in a WebElement.49 The following error occurred:50 InvalidSelectorError: An invalid or illegal selector was specified51 Command duration or timeout: 76 milliseconds52 ...53 *** Element info: {Using=css selector, value=##invalid-locator}54 */55 }56 @Test57 void actionWithoutWaiting_When$Element__WithInvalidLocator() {58 SelenideElement element = $("##invalid-locator");59 try {60 element.exists();61 fail("Expected ElementNotFound");62 } catch (ElementNotFound expected) {63 assertThat(expected)64 .hasMessageStartingWith("Element not found {##invalid-locator}");65 assertThat(expected.getScreenshot())66 .contains(Configuration.reportsFolder);67 assertThat(expected.getCause())68 .isInstanceOf(InvalidSelectorException.class);69 assertThat(expected.getCause())70 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +71 "The following error occurred:\n" +72 "InvalidSelectorError: An invalid or illegal selector was specified");73 }74 //todo - need to fix75 /*76 org.openqa.selenium.InvalidSelectorException:77 The given selector ##invalid-locator is either invalid or does not result in a WebElement.78 The following error occurred:79 InvalidSelectorError: An invalid or illegal selector was specified80 ...81 *** Element info: {Using=css selector, value=##invalid-locator}82 */83 }84 @Test85 void shouldCondition_WhenCollectionElementByIndex_WithInvalidCollectionLocator() {86 SelenideElement element = $$("##invalid-locator").get(0);87 try {88 element.shouldHave(text("Miller"));89 fail("Expected ElementNotFound");90 } catch (ElementNotFound expected) {91 assertThat(expected)92 .hasMessageStartingWith("Element not found {##invalid-locator}");93 assertThat(expected.getScreenshot())94 .contains(Configuration.reportsFolder);95 assertThat(expected.getCause())96 .isInstanceOf(InvalidSelectorException.class);97 assertThat(expected.getCause())98 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +99 "The following error occurred:\n" +100 "InvalidSelectorError: An invalid or illegal selector was specified");101 }102 //todo - need to fix103 /*104 org.openqa.selenium.InvalidSelectorException:105 The given selector ##invalid-locator is either invalid or does not result in a WebElement.106 The following error occurred:107 InvalidSelectorError: An invalid or illegal selector was specified108 ...109 *** Element info: {Using=css selector, value=##invalid-locator}110 */111 }112 @Test113 void shouldCondition_WhenCollectionElementByCondition_WithInvalidCollectionLocator() {114 SelenideElement element = $$("##invalid-locator").findBy(cssClass("the-expanse"));115 try {116 element.shouldBe(exist);117 fail("Expected ElementNotFound");118 } catch (ElementNotFound expected) {119 assertThat(expected)120 .hasMessageStartingWith("Element not found {##invalid-locator}");121 assertThat(expected.getScreenshot())122 .contains(Configuration.reportsFolder);123 assertThat(expected.getCause())124 .isInstanceOf(InvalidSelectorException.class);125 assertThat(expected.getCause())126 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +127 "The following error occurred:\n" +128 "InvalidSelectorError: An invalid or illegal selector was specified");129 }130 //todo - need to fix131 /*132 org.openqa.selenium.InvalidSelectorException:133 The given selector ##invalid-locator is either invalid or does not result in a WebElement.134 The following error occurred:135 InvalidSelectorError: An invalid or illegal selector was specified136 ...137 *** Element info: {Using=css selector, value=##invalid-locator}138 */139 }140 @Test141 void shouldCondition_WhenInnerElement_WithInvalidInnerElementLocator() {142 SelenideElement element = $("ul").find("##invalid-locator");143 try {144 element.shouldBe(exist);145 fail("Expected ElementNotFound");146 } catch (ElementNotFound expected) {147 assertThat(expected)148 .hasMessageStartingWith("Element not found {##invalid-locator}");149 assertThat(expected.getScreenshot())150 .contains(Configuration.reportsFolder);151 assertThat(expected.getCause())152 .isInstanceOf(InvalidSelectorException.class);153 assertThat(expected.getCause())154 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +155 "The following error occurred:\n" +156 "InvalidSelectorError: An invalid or illegal selector was specified");157 }158 //todo - need to fix159 /*160 org.openqa.selenium.InvalidSelectorException:161 The given selector ##invalid-locator is either invalid or does not result in a WebElement.162 The following error occurred:163 InvalidSelectorError: An invalid or illegal selector was specified164 ...165 *** Element info: {Using=css selector, value=##invalid-locator}166 */167 }168 @Test169 void shouldCondition_WhenInnerElement_WithInvalidOuterElementLocator() {170 SelenideElement element = $("##invalid-locator").find(".the-expanse");171 try {172 element.shouldBe(exactTextCaseSensitive("Miller"));173 fail("Expected ElementNotFound");174 } catch (ElementNotFound expected) {175 assertThat(expected)176 .hasMessageStartingWith("Element not found {##invalid-locator}");177 assertThat(expected.getScreenshot())178 .contains(Configuration.reportsFolder);179 assertThat(expected.getCause())180 .isInstanceOf(InvalidSelectorException.class);181 assertThat(expected.getCause())182 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +183 "The following error occurred:\n" +184 "InvalidSelectorError: An invalid or illegal selector was specified");185 }186 //todo - need to fix187 /*188 org.openqa.selenium.InvalidSelectorException:189 The given selector ##invalid-locator is either invalid or does not result in a WebElement.190 The following error occurred:191 InvalidSelectorError: An invalid or illegal selector was specified192 ...193 *** Element info: {Using=css selector, value=##invalid-locator}194 */195 }196 @Test197 void shouldCondition_When$$Collection_WithInvalidLocator() {198 ElementsCollection collection = $$("##invalid-locator");199 try {200 collection.shouldHave(exactTexts("Miller", "Julie Mao"));201 fail("Expected ElementNotFound");202 } catch (ElementNotFound expected) {203 assertThat(expected)204 .hasMessageStartingWith("Element not found {##invalid-locator}");205 assertThat(expected.getScreenshot())206 .contains(Configuration.reportsFolder);207 assertThat(expected.getCause())208 .isInstanceOf(InvalidSelectorException.class);209 assertThat(expected.getCause())210 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +211 "The following error occurred:\n" +212 "InvalidSelectorError: An invalid or illegal selector was specified");213 }214 //todo - need to fix215 /*216 org.openqa.selenium.InvalidSelectorException:217 The given selector ##invalid-locator is either invalid or does not result in a WebElement.218 The following error occurred:219 InvalidSelectorError: An invalid or illegal selector was specified220 ...221 *** Element info: {Using=css selector, value=##invalid-locator}222 */223 }224 @Test225 void actionWithoutWaiting_WhenFilteredCollection_WithInvalidLocator() {226 ElementsCollection collection = $$("##invalid-locator").filter(cssClass("the-expanse"));227 try {228 collection.texts();229 fail("Expected ElementNotFound");230 } catch (ElementNotFound expected) {231 //todo - need to fix232 assertThat(expected)233 .hasMessageStartingWith("Element not found {##invalid-locator}");234 assertThat(expected.getScreenshot())235 .contains(Configuration.reportsFolder);236 assertThat(expected.getCause())237 .isInstanceOf(InvalidSelectorException.class);238 assertThat(expected.getCause())239 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +240 "The following error occurred:\n" +241 "InvalidSelectorError: An invalid or illegal selector was specified");242 }243 /*244 org.openqa.selenium.InvalidSelectorException:245 The given selector ##invalid-locator is either invalid or does not result in a WebElement.246 The following error occurred:247 InvalidSelectorError: An invalid or illegal selector was specified248 ...249 *** Element info: {Using=css selector, value=##invalid-locator}250 */251 }252 @Test253 void shouldCondition_WhenFilteredCollection_WithInvalidLocator() {254 ElementsCollection collection = $$("##invalid-locator").filter(cssClass("the-expanse"));255 try {256 collection.shouldHave(exactTexts("Miller", "Julie Mao"));257 fail("Expected ElementNotFound");258 } catch (ElementNotFound expected) {259 //todo - need to fix260 assertThat(expected)261 .hasMessageStartingWith("Element not found {##invalid-locator}");262 assertThat(expected.getScreenshot())263 .contains(Configuration.reportsFolder);264 assertThat(expected.getCause())265 .isInstanceOf(InvalidSelectorException.class);266 assertThat(expected.getCause())267 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +268 "The following error occurred:\n" +269 "InvalidSelectorError: An invalid or illegal selector was specified");270 }271 /*272 org.openqa.selenium.InvalidSelectorException:273 The given selector ##invalid-locator is either invalid or does not result in a WebElement.274 The following error occurred:275 InvalidSelectorError: An invalid or illegal selector was specified276 ...277 *** Element info: {Using=css selector, value=##invalid-locator}278 */279 }280 @Test281 void shouldCondition_WhenInnerCollection_WithOuterInvalidLocator() {282 ElementsCollection collection = $("##invalid-locator").findAll("li");283 try {284 collection.shouldHave(exactTexts("Miller", "Julie Mao"));285 fail("Expected ElementNotFound");286 } catch (ElementNotFound expected) {287 //todo - need to fix288 assertThat(expected)289 .hasMessageStartingWith("Element not found {##invalid-locator}");290 assertThat(expected.getScreenshot())291 .contains(Configuration.reportsFolder);292 assertThat(expected.getCause())293 .isInstanceOf(InvalidSelectorException.class);294 assertThat(expected.getCause())295 .hasMessageStartingWith("The given selector ##invalid-locator is either invalid or does not result in a WebElement. " +296 "The following error occurred:\n" +297 "InvalidSelectorError: An invalid or illegal selector was specified");298 }299 /*300 org.openqa.selenium.InvalidSelectorException:301 The given selector ##invalid-locator is either invalid or does not result in a WebElement.302 The following error occurred:303 InvalidSelectorError: An invalid or illegal selector was specified304 ...305 *** Element info: {Using=css selector, value=##invalid-locator}306 */307 }308 @Test309 void shouldCondition_WhenInnerCollection_WithInnerInvalidLocator() {310 ElementsCollection collection = $("ul").findAll("##invalid-locator");311 try {312 collection.shouldHave(exactTexts("Miller", "Julie Mao"));313 fail("Expected ElementNotFound");314 } catch (ElementNotFound expected) {315 //todo - need to fix316 assertThat(expected)317 .hasMessageStartingWith("Element not found {ul}");318 assertThat(expected.getScreenshot())319 .contains(Configuration.reportsFolder);320 assertThat(expected.getCause())321 .isInstanceOf(InvalidSelectorException.class);322 assertThat(expected.getCause())323 .hasMessageContaining("##invalid-locator");324 }325 /*326 Element not found {ul}327 Expected: exist328 Screenshot: file:/E:/julia/QA/selenide/build/reports/tests/firefox/integration329 /errormessages/MethodCalledOnEntityWithInvalidLocatorFailsOnTest/330 shouldCondition_WhenInnerCollection_WithInnerInvalidLocator/1471820076618.1.png331 Timeout: 6 s.332 Caused by: NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"ul"}333 */334 }335}...

Full Screen

Full Screen

Source:Corewrappers.java Github

copy

Full Screen

1package org.core;2import org.openqa.selenium.By;3import org.openqa.selenium.InvalidSelectorException;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.support.ui.ExpectedConditions;7import org.openqa.selenium.support.ui.WebDriverWait;8import java.util.concurrent.TimeUnit;9import org.apache.log4j.Logger;10public class Corewrappers {11 public final Logger logger = Logger.getLogger(Corewrappers.class);12 public static WebDriver driver = null;13 public String getLocType(String locator) {14 return (locator.split("~")[0].toLowerCase());15 }16 public String getLocator(String locator) {17 return (locator.split("~")[1]);18 }19 public WebElement getWebElement(String locator) {20 WebElement webElement = null;21 try {22 if (getLocType(locator).equalsIgnoreCase("XPATH")) {23 webElement = driver.findElement(By.xpath(getLocator(locator)));24 } else if (getLocType(locator).equalsIgnoreCase("ID")) {25 webElement = driver.findElement(By.id(getLocator(locator)));26 } else if (getLocType(locator).equalsIgnoreCase("NAME")) {27 webElement = driver.findElement(By.name(getLocator(locator)));28 } else if (getLocType(locator).equalsIgnoreCase("CSS")) {29 webElement = driver.findElement(By.cssSelector(getLocator(locator)));30 } else if (getLocType(locator).equalsIgnoreCase("LINKTEXT")) {31 webElement = driver.findElement(By.linkText(getLocator(locator)));32 }33 } catch (Exception e) {34 logger.error(e);35 }36 return webElement;37 }38 public void click(String locator) throws InterruptedException {39 WebElement w = getWebElement(locator);40 if (w.isDisplayed() || w.isEnabled()) {41 w.click();42 }43 }44 public void login(String username, String password, WebDriver driver) throws InterruptedException {45 //logoWait(driver);46 this.setText("id~username", username);47 this.setText("name~pwd", password);48 this.click("xpath~//*[@id='loginButton']/div");49 }50 public void logoWait(WebDriver driver) throws InvalidSelectorException {51 WebElement ele = driver.findElement(By.className("content-loader"));52 try {53 if (ele.isDisplayed() || ele.isEnabled() || ele.isSelected()) {54 WebDriverWait wait = new WebDriverWait(driver, 30);55 By addItem = By.className("content-loader");56 WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));57 wait.until(ExpectedConditions.stalenessOf(element));58 }59 } catch (InvalidSelectorException e) {60 logger.info("no class found");61 }62 }63 public void progressWait(WebDriver driver) throws InvalidSelectorException {64 WebElement ele = driver.findElement(By.className("pace pace-active"));65 // WebElement ele2=driver.findElement(By.className("pace-progress"));66 try {67 if (ele.isDisplayed() || ele.isEnabled() || ele.isSelected()) {68 // if (ele2.isDisplayed() || ele2.isEnabled() ||69 // ele2.isSelected()) {70 WebDriverWait wait = new WebDriverWait(driver, 30);71 By addItem = By.className("pace pace-active");72 WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));73 wait.until(ExpectedConditions.stalenessOf(element));74 // }75 }76 } catch (InvalidSelectorException e) {77 logger.info("no class found");78 }79 }80 public void alertWait(WebDriver driver) throws InvalidSelectorException {81 WebElement ele = driver.findElement(By.className("alert alert-info"));82 try {83 if (ele.isDisplayed() || ele.isEnabled() || ele.isSelected()) {84 WebDriverWait wait = new WebDriverWait(driver, 30);85 By addItem = By.className("alert alert-info");86 WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));87 wait.until(ExpectedConditions.stalenessOf(element));88 }89 } catch (InvalidSelectorException e) {90 logger.info("no class found");91 }92 }93 public void setText(String locator, String args) {94 WebElement w = getWebElement(locator);95 if (w.isDisplayed()) {96 w.sendKeys(args);97 }98 }99 public void impicitWait(int i) throws InterruptedException{100 driver.manage().timeouts().implicitlyWait(i, TimeUnit.SECONDS);101 }102 103 public void toWait() throws InterruptedException{ ...

Full Screen

Full Screen

Source:ByCssSelectorExtended.java Github

copy

Full Screen

2import java.util.List;3import org.openqa.selenium.By;4import org.openqa.selenium.By.ByCssSelector;5import org.openqa.selenium.InvalidElementStateException;6import org.openqa.selenium.InvalidSelectorException;7import org.openqa.selenium.SearchContext;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebDriverException;10import org.openqa.selenium.WebElement;11import org.openqa.selenium.internal.FindsByCssSelector;12public class ByCssSelectorExtended extends ByCssSelector {13 private static final long serialVersionUID = 1L;14 private String ownSelector;15 private WebDriver driver = null;16 public ByCssSelectorExtended(WebDriver webDriver, String selector) {17 super(selector);18 ownSelector = selector;19 driver = webDriver;20 }21 public static By cssSelector(WebDriver webDriver, final String selector) {22 if (selector == null) {23 throw new IllegalArgumentException(24 "Cannot find elements when the selector is null");25 }26 return new ByCssSelectorExtended(webDriver, selector);27 }28 @Override29 public List<WebElement> findElements(SearchContext context) {30 try {31 if (context instanceof FindsByCssSelector) {32 return ((FindsByCssSelector) context)33 .findElementsByCssSelector(ownSelector);34 }35 } catch (InvalidSelectorException e) {36 return new SizzleSelector(driver).findElementsBySizzleCss(context,37 ownSelector);38 } catch (InvalidElementStateException e) {39 return new SizzleSelector(driver).findElementsBySizzleCss(context,40 ownSelector);41 } catch (WebDriverException e) {42 if (e.getMessage().contains(43 "An invalid or illegal string was specified")) {44 return new SizzleSelector(driver).findElementsBySizzleCss(45 context, ownSelector);46 }47 throw e;48 }49 throw new WebDriverException(50 "Driver does not support finding an element by selector: "51 + ownSelector);52 }53 @Override54 public WebElement findElement(SearchContext context) {55 try {56 if (context instanceof FindsByCssSelector) {57 return ((FindsByCssSelector) context)58 .findElementByCssSelector(ownSelector);59 }60 } catch (InvalidSelectorException e) {61 return new SizzleSelector(driver).findElementBySizzleCss(context,62 ownSelector);63 } catch (InvalidElementStateException e) {64 return new SizzleSelector(driver).findElementBySizzleCss(context,65 ownSelector);66 } catch (WebDriverException e) {67 if (e.getMessage().toLowerCase()68 .contains("An invalid or illegal string was specified")) {69 return new SizzleSelector(driver).findElementBySizzleCss(70 context, ownSelector);71 }72 throw e;73 }74 throw new WebDriverException(...

Full Screen

Full Screen

Source:common.java Github

copy

Full Screen

...4import io.appium.java_client.MobileElement;5import io.appium.java_client.android.AndroidDriver;6import io.github.bonigarcia.wdm.WebDriverManager;7import org.openqa.selenium.By;8import org.openqa.selenium.InvalidSelectorException;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.WebElement;11import org.openqa.selenium.chrome.ChromeDriver;12import org.openqa.selenium.chrome.ChromeOptions;13import org.openqa.selenium.support.ui.ExpectedConditions;14import org.openqa.selenium.support.ui.WebDriverWait;1516import java.util.HashMap;17import java.util.Random;181920public class common {2122 public void scrollToIos(String text) {23 final HashMap<String, String> scrollObject = new HashMap<String, String>();24 scrollObject.put("predicateString", "value =='" + text + "'");25 scrollObject.put("toVisible", "true");26 DriverManager.getDriver().executeScript("mobile: scroll", scrollObject);2728 }2930 public void scrollToEndAndroid() {31 try {32 DriverManager.getDriver().findElement(MobileBy.AndroidUIAutomator(33 "new UiScrollable(new UiSelector().scrollable(true)).scrollToEnd(10)"));34 } catch (InvalidSelectorException e) {35 // ignore36 }37 }3839 public void scrollTillTextAndClick(String text) {40 try {41 ((AndroidDriver<MobileElement>) DriverManager.getDriver()).findElementByAndroidUIAutomator(42 "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"" + text + "\").instance(0))").click();43 } catch (InvalidSelectorException e) {44 // ignore45 }4647 }4849 public void scrollBackward() {50 try {51 DriverManager.getDriver().findElement(MobileBy.AndroidUIAutomator(52 "new UiScrollable(new UiSelector().scrollable(true)).scrollBackward()"));53 } catch (InvalidSelectorException e) {54 // ignore55 }5657 }585960}6162 ...

Full Screen

Full Screen

Source:ExceptionHandling.java Github

copy

Full Screen

1package com.picnic.pack2;2import java.util.Iterator;3import java.util.Set;4import org.openqa.selenium.By;5import org.openqa.selenium.InvalidSelectorException;6//import org.openqa.selenium.NoSuchElementException;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.chrome.ChromeDriver;9public class ExceptionHandling {10 public static void main(String[] args) throws InterruptedException {11 System.setProperty("webdriver.chrome.driver","C:\\\\Users\\\\mohanapriyas\\\\eclipse-workspace-selenium\\\\selenium project\\\\drivers\\\\chromedriver.exe");12 WebDriver driver = new ChromeDriver();13 driver.get("http://demo.guru99.com/popup.php"); 14 driver.manage().window().maximize();15 try {16 driver.findElement(By.xpath("/html/body/p/")).click();17 }18 catch (InvalidSelectorException e) {19 System.out.println("-----------------xpath exception----------------------"); 20 }21 String MainWindow=driver.getWindowHandle(); 22 23 // To handle all new opened window. 24 Set<String> s1=driver.getWindowHandles(); 25 Iterator<String> i1=s1.iterator(); 26 27 while(i1.hasNext()) 28 { 29 String ChildWindow=i1.next(); 30 31 if(!MainWindow.equalsIgnoreCase(ChildWindow)) 32 { ...

Full Screen

Full Screen

Source:Page.java Github

copy

Full Screen

1package page;2import io.qameta.allure.Step;3import org.openqa.selenium.By;4import org.openqa.selenium.InvalidSelectorException;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.support.PageFactory;8import java.util.NoSuchElementException;9public class Page {10 protected WebDriver driver;11 public Page(WebDriver driver) {12 PageFactory.initElements(driver, this);13 this.driver = driver;14 }15 @Step("Инициализация веб-элемента")16 public WebElement getWebelement(WebDriver driver, String locator) {17 WebElement element = null;18 try {19 element = driver.findElement(By.xpath(locator));20 } catch (NoSuchElementException a) {21 System.out.println("Веб-элемент не найден");22 driver.quit();23 } catch (InvalidSelectorException b) {24 System.out.println("Невозможно найти элемент с выражением xpath");25 driver.quit();26 }27 return element;28 }29 @Step("Клик по элементу xpath")30 public void click(WebDriver driver, String locator) {31 getWebelement(driver, locator).click();32 }33 @Step("Ввод текста по локатору xpath")34 public void sendkeys(WebDriver driver, String locator, String text) {35 getWebelement(driver, locator).sendKeys(text);36 }37 @Step("Получение текста из области по xpath")...

Full Screen

Full Screen

Source:HandleAjaxSuggestion.java Github

copy

Full Screen

...3import java.util.List;4import java.util.concurrent.TimeUnit;56import org.openqa.selenium.By;7import org.openqa.selenium.InvalidSelectorException;8import org.openqa.selenium.NoSuchElementException;9import org.openqa.selenium.TimeoutException;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.WebElement;12import org.openqa.selenium.chrome.ChromeDriver;13import org.openqa.selenium.support.ui.ExpectedConditions;14import org.openqa.selenium.support.ui.WebDriverWait;15import org.testng.annotations.Test;1617public class HandleAjaxSuggestion {18 WebDriver driver;19 @Test20 public void AjaxTest(){21 System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");22 driver = new ChromeDriver();23 driver.get("https://www.google.com/");24 driver.manage().window().maximize();25 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);26 driver.findElement(By.name("q")).sendKeys("s");27 WebDriverWait wait = new WebDriverWait(driver, 10); //wait defined 28 try{29 wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@role='listbo']/li[1]")));30 List<WebElement> list = driver.findElements(By.xpath("//*[@role='listbox']/li"));31 System.out.println(list.size());32 33 for(WebElement element : list)34 { 35 if(element.getText().equals("snapdeal"))36 {37 element.click();38 break; //the list will disappear39 }40 }41 }42 catch(InvalidSelectorException e){43 System.out.println("Invalid Selector Exception");44 }45 catch(NoSuchElementException e){46 System.out.println("No Such Element Found");47 }48 catch(TimeoutException e){49 System.out.println("Timeput Exception");50 }5152 driver.close();53 }5455}

Full Screen

Full Screen

Source:XpathHandle.java Github

copy

Full Screen

1package seleniumprojectpack;2import java.util.Iterator;3import java.util.Set;4import org.openqa.selenium.By;5import org.openqa.selenium.InvalidSelectorException;6import org.openqa.selenium.NoSuchElementException;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.chrome.ChromeDriver;9import org.openqa.selenium.firefox.FirefoxDriver;public class XpathHandle { public static void main(String[] args) throws InterruptedException {10System.setProperty("webdriver.chrome.driver","C:\\Users\\rubalakshmim\\eclipse-workspace-selenium\\Selenium\\Driver\\chromedriver.exe");11WebDriver driver=new ChromeDriver();12//Launching the site.13driver.get("http://demo.guru99.com/popup.php");14driver.manage().window().maximize();15// Thread.sleep(3000);16try {17driver.findElement(By.xpath("/html/body/p/a")).click();18}19catch(InvalidSelectorException e){20System.out.println("------------xpath exception---------");21}22String MainWindow=driver.getWindowHandle();23// To handle all new opened window.24Set<String> s1=driver.getWindowHandles();25Iterator<String> i1=s1.iterator();26while(i1.hasNext())27{28String ChildWindow=i1.next();29if(!MainWindow.equalsIgnoreCase(ChildWindow))30{31Thread.sleep(3000);32// Switching to Child window33driver.switchTo().window(ChildWindow);...

Full Screen

Full Screen

InvalidSelectorException

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.InvalidSelectorException;2import org.openqa.selenium.NoSuchElementException;3import org.openqa.selenium.WebDriverException;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.By;6import org.openqa.selenium.Keys;7import org.openqa.selenium.support.ui.ExpectedConditions;8import org.openqa.selenium.support.ui.WebDriverWait;9import org.openqa.selenium.interactions.Actions;10import org.openqa.selenium.JavascriptExecutor;11import org.openqa.selenium.WebElement;12import java.io.File;13import java.io.FileInputStream;14import java.io.BufferedInputStream;15import java.util.Properties;16import java.lang.String;17import java.util.List;18import java.util.Iterator;19import java.util.Date;20import java.text.SimpleDateFormat;21import java.lang.System;22import java.io.IOException;23import java.lang.InterruptedException;24import java.lang.Exception;25import java.lang.Thread;26public class Main {27 public static void main(String[] args) throws Exception {28 Properties prop = new Properties();29 File file = new File("C:\\Users\\Selenium\\Desktop

Full Screen

Full Screen

InvalidSelectorException

Using AI Code Generation

copy

Full Screen

1package com.javatpoint; 2import org.openqa.selenium.By; 3import org.openqa.selenium.WebDriver; 4import org.openqa.selenium.WebElement; 5import org.openqa.selenium.firefox.FirefoxDriver; 6import org.openqa.selenium.support.ui.ExpectedConditions; 7import org.openqa.selenium.support.ui.WebDriverWait; 8public class ExplicitWaitExample { 9public static void main(String[] args) { 10 System.setProperty("webdriver.gecko.driver", "C:\\Users\\shubham\\Desktop\\geckodriver-v0.24.0-win64\\geckodriver.exe"); 11 WebDriver driver=new FirefoxDriver(); 12 driver.manage().window().maximize(); 13 element.click(); 14} 15}16package com.javatpoint; 17import org.openqa.selenium.By; 18import org.openqa.selenium.WebDriver; 19import org.openqa

Full Screen

Full Screen

InvalidSelectorException

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.InvalidSelectorException;2import org.openqa.selenium.WebElement;3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.support.ui.ExpectedConditions;7import org.openqa.selenium.support.ui.WebDriverWait;8import java.util.List;9import java.util.Iterator;10import java.io.File;11import java.io.IOException;12import java.io.PrintWriter;13import java.util.Scanner;14import java.util.concurrent.TimeUnit;15import java.util.concurrent.TimeoutException;16import java.awt.Desktop;17import java.net.URL;18import java.util.ArrayList;19import java.util.Arrays;20import java.util.Collections;21import java.util.HashMap;22import java.util.LinkedList;23import java.util.List;24import java.util.Map;25import java.util.Set;26import java.util.TreeMap;27import java.util.stream.Collectors;28import java.util.stream.Stream;29import java.util.stream.IntStream;30import java.util.stream.StreamSupport;31import java.util.stream.Collectors;32import java.util.concurrent.TimeUnit;33import java.util.concurrent.TimeoutException;34import java.util.regex.Matcher;35import java.util.regex.Pattern;36import java.util.regex.PatternSyntaxException;37import java.util.concurrent.TimeUnit;38import java.util.concurrent.TimeoutException;39import java.util.logging.Level;40import java.util.logging.Logger;41import java.util.stream.Collectors;42import java.util.stream.Stream;43import java.util.stream.IntStream;44import java.util.stream.StreamSupport;45import java.util.stream.Collectors;46import java.util.concurrent.TimeUnit;47import java.util.concurrent.TimeoutException;48import java.util.regex.Matcher;49import java.util.regex.Pattern;50import java.util.regex.PatternSyntaxException;51import java.util.concurrent.TimeUnit;52import java.util.concurrent.TimeoutException;53import java.util.logging.Level;54import java.util.logging.Logger;55import java.util.stream.Collectors;56import java.util.stream.Stream;57import java.util.stream.IntStream;58import java.util.stream.StreamSupport;59import java.util.stream.Collectors;60import java.util.concurrent.TimeUnit;61import java.util.concurrent.TimeoutException;62import java.util.regex.Matcher;63import java.util.regex.Pattern;64import java.util.regex.PatternSyntaxException;65import java.util.concurrent.TimeUnit;66import java.util.concurrent.TimeoutException;67import java.util.logging.Level;68import java.util.logging.Logger;69import java.util.stream.Collectors;70import java.util.stream.Stream;71import java.util.stream.IntStream;72import java.util.stream.StreamSupport;73import java.util.stream.Collectors;74import java.util.concurrent.TimeUnit;75import java.util.concurrent.TimeoutException;76import java.util.regex.Matcher;77import java.util.regex.Pattern;78import java.util.regex.PatternSyntaxException;79import java.util

Full Screen

Full Screen

InvalidSelectorException

Using AI Code Generation

copy

Full Screen

1package com.selenium.examples;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.InvalidSelectorException;7public class InvalidSelectorException {8public static void main(String[] args) {9System.setProperty("webdriver.chrome.driver", "C:/Users/Downloads/chromedriver_win32/chromedriver.exe");10WebDriver driver = new ChromeDriver();11try {12WebElement element = driver.findElement(By.cssSelector("a[href*='login']"));13} catch (InvalidSelectorException e) {14System.out.println("Invalid selector");15}16driver.quit();17}18}19package com.selenium.examples;20import org.openqa.selenium.By;21import org.openqa.selenium.WebDriver;22import org.openqa.selenium.WebElement;23import org.openqa.selenium.chrome.ChromeDriver;24import org.openqa.selenium.InvalidSelectorException;25public class InvalidSelectorException {26public static void main(String[] args) {27System.setProperty("webdriver.chrome.driver", "C:/Users/Downloads/chromedriver_win32/chromedriver.exe");28WebDriver driver = new ChromeDriver();29try {30} catch (InvalidSelectorException e) {31System.out.println("Invalid selector");32}33driver.quit();34}35}

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.

Most used methods in InvalidSelectorException

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