NoSuchElementException
org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.
NoSuchElementException can be thrown in 2 cases as follows :
When using
WebDriver.findElement(By by)://example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));When using
WebElement.findElement(By by)://example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :
Constant Field Type Value
SESSION_ID public static final java.lang.String "Session ID"
e.g. (Session info: chrome=63.0.3239.108)
DRIVER_INFO public static final java.lang.String "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
BASE_SUPPORT_URL protected static final java.lang.String "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
Reason
The reason for NoSuchElementException can be either of the following :
- The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
- The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
- The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
- The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
- The WebElement you are trying to locate is within an
<iframe>tag. - The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use
executeScript()method to scroll the element in to view as follows :WebElement elem = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through
executeScript()method as follows :WebElement element = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element) element.sendKeys("text_to_send");To check if the element is within an
<iframe>traverse up the HTML to locate the respective<iframe>tag andswitchTo()the desired iframe through either of the following methods :driver.switchTo().frame("frame_name"); driver.switchTo().frame("frame_id"); driver.switchTo().frame(1); // 1 represents frame indexHere you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
Reference
You can find Selenium's python client based relevant discussion in:
- Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
NoSuchElementException
org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.
NoSuchElementException can be thrown in 2 cases as follows :
When using
WebDriver.findElement(By by)://example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));When using
WebElement.findElement(By by)://example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :
Constant Field Type Value
SESSION_ID public static final java.lang.String "Session ID"
e.g. (Session info: chrome=63.0.3239.108)
DRIVER_INFO public static final java.lang.String "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
BASE_SUPPORT_URL protected static final java.lang.String "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
Reason
The reason for NoSuchElementException can be either of the following :
- The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
- The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
- The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
- The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
- The WebElement you are trying to locate is within an
<iframe>tag. - The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use
executeScript()method to scroll the element in to view as follows :WebElement elem = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through
executeScript()method as follows :WebElement element = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element) element.sendKeys("text_to_send");To check if the element is within an
<iframe>traverse up the HTML to locate the respective<iframe>tag andswitchTo()the desired iframe through either of the following methods :driver.switchTo().frame("frame_name"); driver.switchTo().frame("frame_id"); driver.switchTo().frame(1); // 1 represents frame indexHere you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
Reference
You can find Selenium's python client based relevant discussion in:
- Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Your code is correct, I suspect the issue caused the page not complete load when you find the element.
Try add a long sleep before find element, if adding sleep worked, change sleep to wait.
Here is the code, It means waiting 10s if the element isn’t present:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "originTextField"))
)
Videos
Looks like timing issue to me. After you redirect to checkout, you might want to wait for the elements before interacting. See Explicit Waits in documentation.
WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("order_billing_name")));
driver.findElement(By.id("order_billing_name")).sendKeys(a.getName());
you must check is this element in and IFRAME if yes then first switch into iframe and secondly if by ID is not working then Use Xpath or CSS path can you please share HTML source with me.
After you clicked loginenter button, some wait should be added to reload page. It will provide small delay which is helpful for SeleniumDriver to identify element. I would like to suggest you to add some condition to wait next element. Please try below code snippet
WebElement myDynamicElement =
(new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("usrUTils")));
You should wait for page load after click on button,so for that write below code :
WebElement element;
Webdriver driver;
WebDriverWait wait = new WebDriverWait(driver, 100);
element= wait.until(ExpectedConditions.elementToBeClickable(By.id("usrUtils")));
As far as on that page Index element is placed in frame, you should use
driver.switchTo().frame("top");
before
driver.findElement(By.linkText("Index")).click();
where top is name attribute of your iframe.
Selenium by default uses top body level context and doesn't see elements in other contexts like iframes or shadow-roots. So to get him to know that you want to search for element in another context, you should change context Selenium is looking into.
And don't forget to switch to defaultContext() back when you returning to body context.
Use below line:
driver.switchTo().frame(0);
(0) - being the first frame on the HTML.
So your last 4 lines of code should be as below:
Thread.sleep(4000);
driver.navigate().forward();
driver.switchTo().frame(0);
driver.findElement(By.linkText("Index")).click();
Note: To switch back to default content, below line of code should be used:
driver.switch_to.default_content()
In your comment you mentioned that the element is within a <frameset> \ <frame>. To work with any element within a frame, you need to first switch the context of the driver from the main page to that frame:
driver.switchTo().frame("foo");
In this example "foo" would be the name of the iframe. You can also do it by index if the frame has no name value.
If you need to switch back to the main page, outside of the frame you would use:
driver.switchTo().defaultContent();
There is a little more detail in the documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp. Look for switchTo().frame
Try using something as follows :-
WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("Username")));
driver.findElement(By.name("Username")).sendKeys("selenium");