Thank you for all answers. I found solution to my problem. The last command was command which was linked with IFrame
WebElement editorFrame = driver.findElement(By.cssSelector("#sentmsgcomposeEditor_ifr"));
driver.switchTo().frame(editorFrame);
WebElement body1 = driver.findElement(By.tagName("body"));
body1.sendKeys(Keys.CONTROL + "a");
So i was in IFrame actually because of it i couldn't find any element. I performed following command:
driver.switchTo().defaultContent();
After that it is possible to find locators.
Answer from Bilow Yuriy on Stack OverflowThank you for all answers. I found solution to my problem. The last command was command which was linked with IFrame
WebElement editorFrame = driver.findElement(By.cssSelector("#sentmsgcomposeEditor_ifr"));
driver.switchTo().frame(editorFrame);
WebElement body1 = driver.findElement(By.tagName("body"));
body1.sendKeys(Keys.CONTROL + "a");
So i was in IFrame actually because of it i couldn't find any element. I performed following command:
driver.switchTo().defaultContent();
After that it is possible to find locators.
You should try to locate by id instead of by xpath.
driver.findElement(By.id("folder0")).click();
Two reasons:
- Locating by id is usually faster (see here for why).
- Since you're trying to test the link, you don't need to click on an inner
<span>, just on the link element itself.
If you still want to use xpath, or still want to get the inner <span>, you can use firebug in Firefox to copy the xpath; this will give you the correct xpath and show you if you made a mistake.

Can't get element by XPATH
selenium - Webdriver Automation - Unable to find element using xpath - Stack Overflow
Selenium Java unable to locate element using xpath - Stack Overflow
Cannot find element by XPATH although it is there.
Videos
Hello guys,
I'm trying to interact with the interative menu of this page in order to automate several data downloads instead of making it by hand.
The thing is that when I copy an XPATH of a selector (for example when I try to get the XPATH of the "Commodities" menu), selenium says:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[9]/div[1]/div[3]/ul/li[6]"}
Does anyone know why I can't get the element?
Thank you all in advance!
EDIT WITH SOLUTION:
The problem was that items I want to find are inside an iframe. So I have to switch the context of the webdriver first. Code with the solution:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.dukascopy.com/swiss/english/marketwatch/historical/')
table = driver.find_element(By.XPATH, '/html/body/div/main/div[2]/div/div/div/p[3]/iframe')
driver.switch_to.frame(table)
driver.find_element(By.XPATH, '/html/body/div[9]/div[1]/div[3]/ul/li[13]').click()Try using CSS
WebElement element = new WebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[id*='DERIVED_REGFRM1_LINK_ADD_ENRL'][class='SSSBUTTON_CONFIRMLINK']")));
It's possible the page object has not loaded at the time Webdriver is searching for it. Try to locate the step that is failing, and place an implicit wait/sleep before it.
If Webdriver is able to find the page object after you know it's a page load issue.
- Identify the step the test is failing on.
- Place a Wait/Sleep before the step ie... 20 seconds (way more than enough).
- Execute your test again.
- If the test now passes, Webdriver searched for the page object before it fully loaded.
Hey guys, new to the community and new to using Selenium. Can I please have some help? I am using Python and Selenium for navigating through a website. The website is turo.com; my goal is to login using my code.
So far, I am able to open the website, navigate to the "log in" button and click on it. After the login button is clicked, there is a modal that pops up with 2 input fields for email/user and password. I have the XPATH to the input fields but they are not working. I get the following error message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"html/body/div[1]/form/div[1]/input[@id="email"]"}
Please let me know how to debug this error. I have tried stackoverflow and other forums but no answer so far. Thank you!
EDIT: I have tried using '//input[@data-testid="email"]' as well.
NoSuchElementException happens generally in 2 scenarios.
- Webelement locator is wrong. (which doesn't seems to be the situation in your case)
- Your are trying to find the webelement even before it is loaded on the page.(This seems to be your problem)
To solve this you should wait till the webelement gets loaded on to the webpage.
One way to do this to use WebDriverWait Class. Sample snippet is below:
/* Initialize the WebDriverWait, with 30 seconds of wait time. */
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("locator_name")));
There might be another reason why you get NoSuchElementException: you are not in the correct iframe. If your element is inside an iframe, you must "move" to it. See https://stackoverflow.com/questions/44834358/switch-to-an-iframe-through-selenium-and-python for an answer in python.
Use linkText element Locator method instead of XPath.
driver.get("http://awqaf.texpo.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element =wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("ABOUT US")));
// WebElement aboutdrop=driver.findElement(By.linkText("ABOUT US"));
Also, try with removing driver.switchTo().defaultContent(); this line.
Generally switchTo().defaultContent(); method is used to go back on previous working window. So if it requires in your case then & then you may use.
You are getting NoSuchElementException which generally happens in 2 cases.
Case 1: The WebElement that you are trying to find is not present (or incorrect) on the page. So check if this is correct By.xpath("//a[contains(text(),'ABOUT US')]")
Case 2: You are trying to find the WebElement even before the page has completed loaded. Which I think is your case. In this scenario you should wait for the WebElement to get completely loaded then find it.
Below is the sample code to wait for the WebElement to load.
//This will initialize a object of WebDriverWait Class with the wait time
//of 30 seconds before throwing the NoSuchElementException.
WebDriverWait wait = new WebDriverWait( driver , 30);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[contains(text(),'ABOUT US')]")));
Let me know if this works for you or not. Cheers.
Your xpath expression:
WebElement query = driver.findElement(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"));
looks correct but if you still are facing the issue please check the correctness of xpath again. If it fails again increase the time for Wait as:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
or you can use explicit wait for the specific element as below:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input")));
Since you want the Polish Google site the
//input[@title='Google Search']
will not work for you. Instead use
//input[@title='Szukaj w Google']
Switch to the iframe first, for that try the below code.
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[src*=sfactory]")));
After that you can use the XPath or CSS selector to identify the locator.
css: a[onclick*=exitAction]
xpath: //a[contains(@onclick, 'exitAction')]
Try with the below xpath.
//a[contains(text(),'Logout')]
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"))
)