This error message...
Sun Sep 22 18:13:27 IDT 2019:ERROR: no such element: Unable to locate element: {"method":"id","selector":"//input[@id='company']"}
(Session info: chrome=76.0.3809.132)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64)
...implies that the ChromeDriver was unable to locate the desired element.
There are a couple of things which you need to take care:
- The Locator Strategy which you have used isn't the
id, but it's the xpath - Though you are using
chrome=76.0butchromedriver=2.36is too old.
Solution
- For the selector value of
//input[@id='company']change the method as"xpath". - Ensure JDK is upgraded to current levels JDK 8u222.
- Ensure Selenium is upgraded to current levels Version 3.141.59.
- Ensure ChromeDriver is updated to current ChromeDriver v77.0 level.
- Chrome is updated to current Chrome Version 77.0 level. (as per ChromeDriver v77.0 release notes)
Im trying to challenge myself by making selenium redeem 1 gamepass code on microsoft issue is I Found the ID but it doesn't work as in Selenium can't find it, This is the website I need selenium to recognize and type in it
this is the error
Traceback (most recent call last):
File "c:\Users\jeans\Downloads\New folder\Microsoft\redeem.py", line 30, in <module>
gamepass = driver.find_element(By.ID, value="tokenString")
File "C:\Users\jeans\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\jeans\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "C:\Users\jeans\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="tokenString"]"}
(Session info: chrome=104.0.5112.81)
Stacktrace:
Backtrace:
Ordinal0 [0x00FA78B3+2193587]
Ordinal0 [0x00F40681+1771137]
Ordinal0 [0x00E541A8+803240]
Ordinal0 [0x00E824A0+992416]
Ordinal0 [0x00E8273B+993083]
Ordinal0 [0x00EAF7C2+1177538]
Ordinal0 [0x00E9D7F4+1103860]
Ordinal0 [0x00EADAE2+1170146]
Ordinal0 [0x00E9D5C6+1103302]
Ordinal0 [0x00E777E0+948192]
Ordinal0 [0x00E786E6+952038]
GetHandleVerifier [0x01250CB2+2738370]
GetHandleVerifier [0x012421B8+2678216]
GetHandleVerifier [0x010317AA+512954]
GetHandleVerifier [0x01030856+509030]
Ordinal0 [0x00F4743B+1799227]
Ordinal0 [0x00F4BB68+1817448]
Ordinal0 [0x00F4BC55+1817685]
Ordinal0 [0x00F55230+1856048]
BaseThreadInitThunk [0x76CEFA29+25]
RtlGetAppContainerNamedObjectPath [0x779B7A9E+286]
RtlGetAppContainerNamedObjectPath [0x779B7A6E+238]
NoSuchElementException, Selenium unable to locate element - Stack Overflow
java - Unable to locate an Element By ID in Selenium - Stack Overflow
java - Selenium unable to locate element by id/name - Stack Overflow
Selenium cannot locate any elements
Videos
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"))
)
<iframe id="login-iframe" title="Login" src="https://www.barclaycardus.com/servicing/authenticate/home?rnd=120231985&xsessionid=FF4CC3BDD157751E5B9AF5E756D3E303" frameborder="0"></iframe>
You have an iframe switch to it.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "login-iframe")))
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Ther is an iframe so you have to switch to it
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def login(url, usernameId, username, passwordId, password, submit_buttonId):
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "login-iframe")))
time.sleep(5)
driver.find_element_by_id(usernameId).send_keys(username) #username input box not being identified
driver.find_element_by_id(passwordId).send_keys(password)
driver.find_element_by_xpath(submit_buttonId).click()
login(
'https://cards.barclaycardus.com/',
'username', myBarclaysUsername,
'password', myBarclaysPassword,
'loginButton'
)
EDIT:
I solved it thanks to the help of the redditors here. Turns out, it had a dynamic id, which changes each time. I had to use the name in order to properly identify it, like this:
driver.findElement(By.name("firstname"));Hello
I'm trying to learn Selenium, and thus I have used a website designed for that. After trying a few different things, I have a user flow almost ready. However, when it comes to the checkout (you can find it at https://magento.softwaretestingboard.com/checkout/#shipping ), I try to select the different fields in order to input data in them, after the first one (email, the only ID with actual words in it), it doesn't recognize the IDs, despite me double and triple checking to make sure they are correctly entered.
Googling my problem, I read it could be because some elements haven't fully loaded, but even after using sleep(10) it still doesn't find them, throwing the error "Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="XS4WH19"]"}".
Not sure if it's helpful, but here's what I get when I inspect the element.
<input class="input-text" type="text" data-bind="
value: value,
valueUpdate: 'keyup',
hasFocus: focused,
attr: {
name: inputName,
placeholder: placeholder,
'aria-describedby': getDescriptionId(),
'aria-required': required,
'aria-invalid': error() ? true : 'false',
id: uid,
disabled: disabled
}" name="firstname" aria-required="true" aria-invalid="false" id="XS4WH19">Thanks a lot for your help :)
First correct your syntax error:
driver.findElement(By.id("diagnosisivd")).click();If this is correct and still your are facing the same then try by adding some wait at some conditions.
WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("diagnosisivd")));Try with different locator type XPath/CSSSelector-
By.xpath : //*[@id='diagnosisivd']
Even if I don't know anything about it, what you can try is to run the test in debug mode and set a breakpoint on that instruction. If once you run it you wait some time before executing the instruction in the breakpoint and it works, then it's a matter of timing. Probably the element is not there yet when you try to click on it. Otherwise you can put an explicit wait for something like 5 seconds before that instruction and check if it works. If it works, remove the explicit wait and make the wait depending on the presence and clickability of that element.
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")));