How do I refer to this control in Selenium? - selenium

I wrote this
driver.findElement(By.xpath("//input[#type='textarea']")).sendKeys("foo");
and I got this
BrowserController.remoteControl(): Unable to locate element: //input[#type='textarea']

When you are not able to find the element it could be following reasons
Identifier you provided is wrong
element is inside some frame, in this case you will have to find that frame and switch to it.
driver.switchTo().frame(1);
website is taking sometime to load the element. in this case you could you explicit wait.
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id"));

Related

Getting "Nosuch element Exception" in Selenium Though XPATH is correct. Not sure is this due to Shadow DOM. Please confirm

I am trying to automate Salesforce application Using Selenium and getting NoSuchelementException though XPATH is correct and valid for particular object. When i have searched the issue, it might be reason for Shadow DOM.
For EX:
So XAPTH i have written like,
driver.findElement(By.xpath("//input[#name='Name']")).sendKeys("Jams");
or
driver.findElement(By.xpath("//input[#id='input-299']")).sendKeys("Jams");
This XPATH is highlighting in Console as well. But while automating it throws nosuchelement error.
So while checking for ShadowDOM option, am getting option like this for Name Object.
#shadow-root(user-agent)
Shadowroot DIV
-- nothing mentioned in div. it just open and closed tags.
How to automate this?
You can check if there are any iframes in your Dom. Simply do //iframe in your page developer mode(F12)> elements tab > search (Ctrf+F) area. If there are any, you will get the number of iframes.
Now if your textbox is in any of iframe use below code to go inside particular iframe first
driver.switch_to.frame("<name or Id of frame>")
then come out to frame use below:
driver.switch_to.parent_frame()
Also, if the issue is not related to frames check below for shadow-root related issue:
you can check below for shadow-root element ( Question is for Java, but you can co-relate):
How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector
The website contents are mostly created now using javascript. You may have to wait for certain elements to load before doing anything to it.
https://seleniumbyexamples.github.io/wait

Selenium cssselector shows correct webelement but script always run exception no such element

I got stuck not just for this one case. Many cases came out with nosuchelement exception, I cannot find the reason why. I use chrome console to locate the element and it shows correct result. Is that possible something wrong with the page itself, not my script?
Context click on the element you are trying to click, and see if the element is inside any frame. Try to navigate through the tags and see if this element is under any iframe.
switch to the iframe using,
driver.switchTo().frame(driver.findElement(By.xpath("iframexpath")));

can not find web element using webdriver?

I'm trying to find elements on below URL to perform some task, but I could not found any element on this page. So, kindly help me.
URL: https://cdns.webex.com/mw3000/mywebex/default.do?siteurl=cdns
My code is:
WebElement element = driver.findElement(By.xpath("html/body/div[1]/div[3]/table/tbody/tr/td[7]/span/a"));
System.out.println(element.getAttribute("id"));
Error which I faced :
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element.
The page you specified is made up of multiple frames. The element you are searching for is inside one of the frames. I'm not particularly familiar with Selenium, so I don't know if it supports going across frame boundaries like that, but that's where you need to start looking at least.

Unable to find web elments

In below screenshot you can see that I am trying to identify the password field with a valid xpath:
(.//*[#id='loginForm']/div/div/div/div/input)
No elements is identified but if I use the inspect element button and find any element randomly and then use the above xpath expression it is working.
Screenshot 1
Screenshot 2
I got your point.
The thing is if you start the firepath and pasted
//*[#id='loginForm']/div/div1/div/div/input
above xpath, it tried to find that element if that is available in default or main page. If your element is inside any iframe then it will not able to find it.
If you inspect that element or any other near by, if they are inside same frame then your firepath is inside or focus on to that frame. If pasted your above xpath then it will able to find the element because you alreay in focus of that frame.
As per screen if you observe firepath left side, you are able to find some thing like iframe#iframe means your firepath is in focus to that frame and elements in that frame able to find by pasting.
At the time of starting the firepath it may like top window or widows and you pasted any element available in frame, then it is expected that it will not able to find it.
I hope you got it.
Thank You,
Murali
As murali said element was present inside IFRAME i changed the focus to iframe now it is working properly

selenium invalid element state: Element is not currently interactable and may not be manipulated

I have this issue with selenium; I cannot find i textinput : it always raises this exeception:
Element is not currently interactable and may not be manipulated
in this line:
Driver.FindElement(By.Id("ctl00")).Clear();
I try to put waiting like this:
Waiting.Until(driver =>(By.Id("ctl00")));
and
Waiting.Until(ExpectedConditions.ElementExists(By.Id("ctl00")));
but no luck.
If the exception gets thrown on Clear(),
the element is most likely present on the page and the input in a readonly state.
Try to validate XPath first as might be that targeting more than one element.
Due to this, element not currently interactable and may not be manipulated.
I faced same issue and found solution with above as my XPath was locating multiple element which exists on DOM but not page.
Correct the element XPath, error will resolve automatically with adding webdriver wait.
I hit the same error, was also a bit confused, but resolved it using the below wait condition. The elementToBeClickable condition checks that the element is both visible and enabled. Perhaps visibility is enough, but Selenium throws an explicit ElementNotVisibleException, so I'm not sure why I wouldn't get that...
Anyways, I opted to use a more robust wait condition, and it has worked well for me.
wait.until(ExpectedConditions.elementToBeClickable(By.id("myElementId"))).clear();
This happens sometimes when element is not visible. It would happen even when element is visible also.
Try using with different tags like xpath or etc.
You can check the element status using below script
driver.findElement(By.className("")).isEnabled()
It is due to element not visible so apply more wait while performing the action or you can use hardcoded sleep cause sometimes webdriver's wait doesn't work.
You are trying to perform .clear() on a WebElement, which is not an input tag. For example, if you try to do clear on a Span WebElement, You will get Selenium Invalid Element State error.
Waiting.Until(ExpectedConditions.ElementExists(By.Id("ctl00"))); should work.
Set break point at Waiting.Until() line and input $$("#ctl00") in Chrome console to check if the element is existing.
Make the driver to wait until an element is visible.
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOf(element));
Hope this helps!
If waiting for visibilityOf(), elementToBeClickable(), element.isEnabled() doesn't do the trick, try to look at the attributes of your element.
It may have some attributes responsible for read only state, in my case this was aria-readonly and aria-disabled, Hence I created custom wait like this:
webDriverWait.until(webDriver -> webElement.getAttribute("aria-readonly").equals("false") && webElement.getAttribute("aria-disabled").equals("false"));
Use this you will not get this error(might be scroll is not working here so use keypad TAB to move on, my problem got resolved by this)
Actions act = new Actions(driver);
act.sendKeys(Keys.TAB).build().perform();
act.sendKeys(Keys.RETURN).build().perform();
You might be asking an element with the same attribute like duplicate id, name etc.
I My case i was entering password in confirm password TextBox whose Id was "confirmPassword" but the same Id is used for the "Confirm Password" label for that TextBox.
So selenium was trying to perform sendKeys on the label instead of TextBox so getting "InvalidElementStateException", After that i changed the xpath and it worked.
So when you get "InvalidElementStateException" you should first check whether there are any duplicate element available with the xpath you have generated, and that not-interactive element could be a Button, Label or disabled element that causes the exception occurred.
I was clicking on a div element or label to element to clear the text in input tag. Changed my xpath to input tag and then I did not received any exception and the test was clear.