I've been working on a company project involving Selenium
and I keep getting NoSuchElementException
despite putting xpath
, cssSelector
and linkText
. Below is my program.
NewCard.java
public class NewCard {
public static void createTopUpRequestNewCard(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement utilityTopUp = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='wrapper']/div/ul/li[9]/a")));
utilityTopUp.click();
WebElement createTopUpNewCard = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='wrapper']/div/div/ul/li[2]/a")));
createTopUpNewCard.click();
}
}
Error StackTrace
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //div[@id='wrapper']/div/ul/li[9]/a (tried for 30 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at AbtMainTestControl.AutomationTesting.newcard.NewCard.createTopUpRequestNewCard(NewCard.java:41)
at AbtMainTestControl.AutomationTesting.test.AutoTest.createTopUpRequest(AutoTest.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:63)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id='wrapper']/div/ul/li[9]/a"}
(Session info: chrome=87.0.4280.141)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'EZL-OA-JACQUELI', ip: '192.168.130.18', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_241'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.141, chrome: {chromedriverVersion: 87.0.4280.88 (89e2380a3e36c..., userDataDir: C:\Users\lukegoh\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:58598}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 78c5d3b2ff7e0a99e3da6b4bba18815b
*** Element info: {Using=xpath, value=//div[@id='wrapper']/div/ul/li[9]/a}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at org.openqa.selenium.support.ui.ExpectedConditions7ドル.apply(ExpectedConditions.java:205)
at org.openqa.selenium.support.ui.ExpectedConditions7ドル.apply(ExpectedConditions.java:201)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
... 34 more
HTML Code
Utility Top Up
<a href="#" class="menu__link"><svg aria-hidden="true" class="svg-inline--fa fa-user fa-w-14 menu__icon" focusable="false" data-prefix="fa" data-icon="user" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" data-fa-i2svg=""><path fill="currentColor" d="M224 256c70.7 0 128-57.3 128-128S294.7 0 224 0 96 57.3 96 128s57.3 128 128 128zm89.6 32h-16.7c-22.2 10.2-46.9 16-72.9 16s-50.6-5.8-72.9-16h-16.7C60.2 288 0 348.2 0 422.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-41.6c0-74.2-60.2-134.4-134.4-134.4z"></path></svg><!-- <i aria-hidden="true" class="fa fa-user menu__icon"></i> --> Utility Top Up <svg aria-hidden="true" class="svg-inline--fa fa-chevron-right fa-w-10 menu__arrow-icon" focusable="false" data-prefix="fa" data-icon="chevron-right" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" data-fa-i2svg=""><path fill="currentColor" d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"></path></svg><!-- <i aria-hidden="true" class="fa fa-chevron-right menu__arrow-icon"></i> --></a>
Screenshot of Utility Top Up
locators
Create Top Up New Card
<a href="#" class="context-menu__link"> create top up request (new card) </a>
Screenshot of Create Top Up New Card
locators
The screenshots above are the locators of the 2 elements I'm interacting respectively. I used Selenium IDE
to record my program. Nevertheless, despite trying all these locators, I keep getting the NoSuchElementException
. May I know how to solve these recurring issues?
-
sqa.stackexchange.com/questions/41860/…Niels van Reijmersdal– Niels van Reijmersdal2021年01月16日 16:18:29 +00:00Commented Jan 16, 2021 at 16:18
-
please add your htmlPDHide– PDHide2021年01月17日 09:51:16 +00:00Commented Jan 17, 2021 at 9:51
-
what happens if you use presence of element instead of located by ?PDHide– PDHide2021年01月17日 09:52:21 +00:00Commented Jan 17, 2021 at 9:52
-
@PDHide You mean replace VisibilityofElementLocated with PresenceofElementLocated? Still doesn't make a differenceTechGeek49– TechGeek492021年01月17日 09:57:38 +00:00Commented Jan 17, 2021 at 9:57
-
2We cannot help with just a small part of the HTML/DOM.Niels van Reijmersdal– Niels van Reijmersdal2021年01月17日 16:03:31 +00:00Commented Jan 17, 2021 at 16:03
3 Answers 3
Update
use:
//*[text()[contains(.,"Utility Top Up")]]
use wait :
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()[contains(.,"Utility Top Up")]]")));
The text is inside anchor tag but there is a tag in between text so , first get text contents using text() and check for utility Top in all the retrieved text content.
Old
If you are not getting element not found error after changing VisibilityofElementLocated with PresenceofElementLocated , then it means selenium is not able to identify the element . Unless you provide the html it is hard to answer . THe only thing to recommend is :
Your locator might be wrong , verify if locator is correct by using dev tool ( Inpect element)
Element might be inside iframe , if so you should switch to frame first before you can find or interact with those elements
Element might be a shadow element , you should use javascript
shadowRoot and queryselector scripts to find such shadow root elements.
-
I have included HTML Images so it would be neaterTechGeek49– TechGeek492021年01月17日 10:41:25 +00:00Commented Jan 17, 2021 at 10:41
-
I have included HTML CodeTechGeek49– TechGeek492021年01月18日 13:54:42 +00:00Commented Jan 18, 2021 at 13:54
-
@TechGeek49 for each elementent are you getting no element found ? utilityTopUp or createTopUpNewCardPDHide– PDHide2021年01月18日 14:04:51 +00:00Commented Jan 18, 2021 at 14:04
-
In particular utilityTopUp. If that doesn't work, then createTopUpNewCard navigation doesn't work as wellTechGeek49– TechGeek492021年01月18日 14:06:01 +00:00Commented Jan 18, 2021 at 14:06
-
since createTopUp is a subsection of UtilityTopUp navigation menuTechGeek49– TechGeek492021年01月18日 14:06:29 +00:00Commented Jan 18, 2021 at 14:06
You have twice the same selector: By.xpath("//div[@id='wrapper']/div/ul/li[9]/a")
Are you sure the second time the selector is exact the same? In my experience new elements that are created after a click have a different selector.
I would try a more simple selector like: By.partialLinkText("Utility Top Up")
If you want a better answer I need the following info:
- HTML/DOM sources of the part you are interacting with
- Is it the first wait or the second that cannot find the element
-
1st wait I couldn't find itTechGeek49– TechGeek492021年01月16日 16:33:19 +00:00Commented Jan 16, 2021 at 16:33
-
Maybe it is in a frame? sqa.stackexchange.com/questions/41860/…Niels van Reijmersdal– Niels van Reijmersdal2021年01月16日 17:55:59 +00:00Commented Jan 16, 2021 at 17:55
-
It's not in a frame. Just checkedTechGeek49– TechGeek492021年01月17日 08:06:10 +00:00Commented Jan 17, 2021 at 8:06
-
I have included HTML images for the portion I'm dealing withTechGeek49– TechGeek492021年01月17日 10:45:19 +00:00Commented Jan 17, 2021 at 10:45
-
Why not use a selector like: By.partialLinkText("Utility Top Up")Niels van Reijmersdal– Niels van Reijmersdal2021年01月17日 15:58:26 +00:00Commented Jan 17, 2021 at 15:58
try using Actions class :
Actions action=new Actions(driver); action.moveToElement(utilityTopUp).bulid().perform(); action.moveToElement(utilityTopUp).click().build().perform();
-
This doesn't even workTechGeek49– TechGeek492021年01月18日 14:56:18 +00:00Commented Jan 18, 2021 at 14:56
Explore related questions
See similar questions with these tags.