element not interactable exception in selenium web automation - java

In the below code i cannot send password keys in the password field, i tried clicking the field, clearing the field and sending the keys. But now working in any of the method. But its working if i debug and test
public class TestMail {
protected static WebDriver driver;
protected static String result;
#BeforeClass
public static void setup() {
System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
#Test
void Testcase1() {
driver.get("http://mail.google.com");
WebElement loginfield = driver.findElement(By.name("Email"));
if(loginfield.isDisplayed()){
loginfield.sendKeys("ragesh#gmail.in");
}
else{
WebElement newloginfield = driver.findElemnt(By.cssSelector("#identifierId"));
newloginfield.sendKeys("ragesh#gmail.in");
// System.out.println("This is new login");
}
driver.findElement(By.name("signIn")).click();
// driver.findElement(By.cssSelector(".RveJvd")).click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// WebElement pwd = driver.findElement(By.name("Passwd"));
WebElement pwd = driver.findElement(By.cssSelector("#Passwd"));
pwd.click();
pwd.clear();
// pwd.sendKeys("123");
if(pwd.isEnabled()){
pwd.sendKeys("123");
}
else{
System.out.println("Not Enabled");
}

Try setting an implicit wait of maybe 10 seconds.
gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Or set an explicit wait. An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. In your case, it is the visibility of the password input field. (Thanks to ainlolcat's comment)
WebDriver gmail= new ChromeDriver();
gmail.get("https://www.gmail.co.in");
gmail.findElement(By.id("Email")).sendKeys("abcd");
gmail.findElement(By.id("next")).click();
WebDriverWait wait = new WebDriverWait(gmail, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
gmail.findElement(By.id("Passwd")).sendKeys("xyz");
Explanation: The reason selenium can't find the element is because the id of the password input field is initially Passwd-hidden. After you click on the "Next" button, Google first verifies the email address entered and then shows the password input field (by changing the id from Passwd-hidden to Passwd). So, when the password field is still hidden (i.e. Google is still verifying the email id), your webdriver starts searching for the password input field with id Passwd which is still hidden. And hence, an exception is thrown.

"element not interactable" error can mean two things :
a. Element has not properly rendered:
Solution for this is just to use implicit /explicit wait
Implicit wait :
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
Explicit wait :
WebDriverWait wait=new WebDriverWait(driver, 20);
element1 = wait.until(ExpectedConditions.elementToBeClickable(By.className("fa-stack-1x")));
b. Element has rendered but it is not in the visible part of the screen:
Solution is just to scroll till the element. Based on the version of Selenium it can be handled in different ways but I will provide a solution that works in all versions :
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView(true);", element1);
Suppose all this fails then another way is to again make use of Javascript executor as following :
executor.executeScript("arguments[0].click();", element1);
If you still can't click , then it could again mean two things :
1. Iframe
Check the DOM to see if the element you are inspecting lives in any frame. If that is true then you would need to switch to this frame before attempting any operation.
driver.switchTo().frame("a077aa5e"); //switching the frame by ID
System.out.println("********We are switching to the iframe*******");
driver.findElement(By.xpath("html/body/a/img")).click();
2. New tab
If a new tab has opened up and the element exists on it then you again need to code something like below to switch to it before attempting operation.
String parent = driver.getWindowHandle();
driver.findElement(By.partialLinkText("Continue")).click();
Set<String> s = driver.getWindowHandles();
// Now iterate using Iterator
Iterator<String> I1 = s.iterator();
while (I1.hasNext()) {
String child_window = I1.next();
if (!parent.equals(child_window)) {
driver.switchTo().window(child_window);
element1.click()
}

Please try selecting the password field like this.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement passwordElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#Passwd")));
passwordElement.click();
passwordElement.clear();
passwordElement.sendKeys("123");

you may also try full xpath, I had a similar issue where I had to click on an element which has a property javascript onclick function. the full xpath method worked and no interactable exception was thrown.

In my case the element that generated the Exception was a button belonging to a form. I replaced
WebElement btnLogin = driver.findElement(By.cssSelector("button"));
btnLogin.click();
with
btnLogin.submit();
My environment was chromedriver windows 10

In my case, I'm using python-selenium.
I have two instructions. The second instruction wasn't able to execute.
I put a time.sleep(1) between two instructions and I'm done.
If you want you can change the sleep amount according to your need.

I had the same problem and then figured out the cause. I was trying to type in a span tag instead of an input tag. My XPath was written with a span tag, which was a wrong thing to do. I reviewed the Html for the element and found the problem. All I then did was to find the input tag which happens to be a child element. You can only type in an input field if your XPath is created with an input tagname

I'm going to hedge this answer with this: I know it's crap.. and there's got to be a better way. (See above answers) But I tried all the suggestions here and still got nill. Ended up chasing errors, ripping the code to bits. Then I tried this:
import keyboard
keyboard.press_and_release('tab')
keyboard.press_and_release('tab')
keyboard.press_and_release('tab') #repeat as needed
keyboard.press_and_release('space')
It's pretty insufferable and you've got to make sure that you don't lose focus otherwise you'll just be tabbing and spacing on the wrong thing.
My assumption on why the other methods didn't work for me is that I'm trying to click on something the developers didn't want a bot clicking on. So I'm not clicking on it!

I got this error because I was using a wrong CSS selector with the Selenium WebDriver Node.js function By.css().
You can check if your selector is correct by using it in the web console of your web browser (Ctrl+Shift+K shortcut), with the JavaScript function document.querySelectorAll().

If it's working in the debug, then wait must be the proper solution.
I will suggest to use the explicit wait, as given below:
WebDriverWait wait = new WebDriverWait(new ChromeDriver(), 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#Passwd")));

I came across this error too. I thought it might have been because the field was not visible. I tried the scroll solution above and although the field became visible in the controlled browser session I still got the exception. The solution I am committing looks similar to below. It looks like the event can bubble to the contained input field and the end result is the Selected property becomes true.
The field appears in my page something like this.
<label>
<input name="generic" type="checkbox" ... >
<label>
The generic working code looks more or less like this:
var checkbox = driver.FindElement(By.Name("generic"), mustBeVisible: false);
checkbox.Selected.Should().BeFalse();
var label = checkbox.FindElement(By.XPath(".."));
label.Click();
checkbox.Selected.Should().BeTrue();
You'll need to translate this to your specific language. I'm using C# and FluentAssertions. This solution worked for me with Chrome 94 and Selenium 3.141.0.

I had to hover over the element first for the sub-elements to appear. I didn't take that into account at first.
WebElement boardMenu = this.driver.findElement(By.linkText(boardTitle));
Actions action = new Actions(this.driver);
action.moveToElement(boardMenu).perform();
Another tip is to check that you are having one element of that DOM. Try using Ctrl+F when inspecting the web page and check your xpath there; it should return one element if you are going with the findElement method.

Related

How to wait for an element to be visible in Selenium newer versions, using Java?

I started learning Selenium lately and i wrote a simple code which uses Selenium 3.2.2, and waits for an email field to load in order to fill it:
WebDriverWait wait=new WebDriverWait(driver,25);
WebElement login = driver.findElement(By.xpath("//*[#id=\"Login\"]/div/div/button[1]"));
login.click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"email\"]")));
Ive updated to a new Selenium version and since then I cant get it to work- the last row now resuqires a function, how can i write a function that waits for an web element to be loaded? thanks
Iv found those ExpectedConditions for 'visibility' and 'clickable' are not as reliable as doing something like this:
Wait = new WebDriverWait(getDriver(), 15);
public WebElement waitUntilElementIsReady(By selector)
{
WebElement element = null;
Wait.until(d ->
{
element = d.findElement(selector));
return element.isDisplayed();
});
return getDriver().findElement(selector);
}
This will work in the almost every scenario but there will be times when you are trying to get an element that is not displayed. This may be for some information or to use that element to find another element. I have found this is pretty rare if the application has elements with unique ID's.

Unable to send keys to div element Selenium - Java

Hi am trying to populate value to text box (check image below) using xpath.
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.xpath("//*[#class='CzI8E']")));
actions.click();
Thread.sleep(3000);
actions.moveToElement(driver.findElement(By.xpath("//*[#class='_2S1VP copyable-text selectable-text']")));//_2S1VP copyable-text selectable-text
actions.sendKeys(WhatsappConstants.TEXT_MESSAGE);
actions.build().perform();
But i am getting this exception
org.openqa.selenium.WebDriverException: unknown error: ChromeDriver only supports characters in the BMP
Other stackoverflow answers said to use firefox driver but in my case i need you use chrome only.
This is a known limitation of the Chromedriver, see http://crbug.com/chromedriver/2269 for the bug report in the official bug tracker.
What you could do is to only limit yourself to supported characters, basically those from: http://www.columbia.edu/kermit/ucs2.html
Alternatively, you could simulate input instead of really sending the keys using front-end JS snippet like the following:
(function (element, text) {
Array.prototype.forEach.call(text, function (char) {
element.value += char;
element.dispatchEvent(new KeyboardEvent("keydown"));
element.dispatchEvent(new KeyboardEvent("keypress"));
element.dispatchEvent(new KeyboardEvent("input"));
element.dispatchEvent(new KeyboardEvent("keyup"));
});
}).apply(null, arguments);
Which you then call using the JavascriptExecutor:
((JavascriptExecutor) driver).executeScript(JS_CODE, element, text);
The snippet works on elements with a writable .value property, it could be extended to support contenteditable elements.
Note that the fields of the events are set to their defaults, including key codes and such, see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent Also you might have to add additional events to be triggered to even better simulate user input.
I have a similar problem and I solve it in this way.
First, you should add any text to this div with JavascriptExecutor , and then clear this text and use selenium's sendKeys method
WebElement inputDiv = driver.findElement(By.xpath("your_path_to_div"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("document.querySelector('your_selector_to_div').innerText='aaa'");
inputDiv.clear();
inputDiv.sendKeys(subject);

Selenium Gmail login password field

I am trying to login Gmail using selenium web driver. The problem I am facing is that I am not able to set the password in the input box.
Here is the generated error message:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element.
Here is my code:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://www.gmail.com");
driver.findElement(By.cssSelector("#identifierId")).sendKeys("********#gmail.com");
driver.findElement(By.cssSelector("#identifierNext")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#password")));
driver.findElement(By.cssSelector("#password")).sendKeys("********");
driver.findElement(By.cssSelector("#passwordNext")).click();
driver.close();
driver.quit();
}
Wait until an element becomes clickable. Here is how you can do this.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=password]"));
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("your password");
This could be a combination of a wait issue and selecting the wrong element.
Try changing your selector in your sendKeys as follows:
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("********");
If that still doesn't work, you could try a different wait condition before that call, such as:
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=password]")));
You might need to do some experiments to find the right wait condition, but doing both of those things should get you what you need. :)
The cssSelector of the element you are trying to use is not right.
I would suggest to use dynamically generated xpath and/or cssSelector all the time.
In the code below, I used dynamically generated xpath for email Id and cssSelector for password. Try this and it works fine.
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "Y:\\Selenium\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://www.gmail.com");
driver.findElement(By.xpath("//input[#type='email']")).sendKeys("*********#gmail.com");
driver.findElement(By.cssSelector("#identifierNext")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[type=password]")));
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("*********");
driver.findElement(By.cssSelector("#passwordNext")).click();
driver.quit();
}
You can also use the name atribute here, like this:
driver.findElement(By.name("password")).sendKeys("********");
I think everything is solved, however I would like to clarify why it was not working:
To enter the email, You used an id and it worked fine, because the id identifierId is one of the attribute of the filed you are filling.
To enter the password, you cannot use password as an id because the field you are trying to fill has no id among its attributes. The id password exists, but it embraces a larger amount of WebElement. In fact you are localizing an id which contains this field among other WebElements, hence the error message telling you it is unable to focus
you can either use xpath, such as
driver.findElement(By.xpath("//input[#type='password']")).sendKeys("********");
or css selector such as:
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("********");
but you cannot access this field using an ID.
I tried all those three solutions and they worked.
Regarding the possible waiting issue, do not hesitate to use wrappers with waiter around findElement method, it can help a lot. However, I think it was not the main issue here.

Action class works in debug mode in selenium webdriver

I have written the code and that works well when I run this is debugging mode but when I run it in normal mode then I am getting the following exception
org.openqa.selenium.NoSuchElementException: no such element:
Unable to locate element: {"method":"xpath","selector":".//*[#id='address-0']/span"}
The code I have written is:
WebElement searchBox = driver.findElement(By.id("search-input"));
searchBox.sendKeys("somepostcode");
Actions actions = new Actions(driver);
actions.moveToElement(searchBox);
WebElement address = driver.findElement(By.xpath(".//*[#id='address-0']/span"));
actions.moveToElement(address);
actions.click();
actions.perform();
I am not able to understand where should I put wait.
I am using eclipse IDE. The functionality works like when I put some postcode in the search box it search for some addresses at runtime and the user has to select any address related to the postcode. Ajax has been used to fetch the postcode
Here search box is a textbox.
Please let me know if more information is required.
Try adding some wait time before WebElement address = driver.findElement(By.xpath(".//*[#id='address-0']/span"));
Error tells you that, you are trying to create an instance of WebElement "address" before its visible on the page.
Try adding wait before
WebElement address = driver.findElement(By.xpath(".//*[#id='address-0']/span"));
In cases like this, when the script works in debug mode but fails during normal it is almost always the issue with timing. So your page is just not fully loaded at the time you are trying to locate that element.
Place an explicit wait just before your problematic element. It's usually not the best practice to use explicit wait but you can do it as a quick try to see if that solves your problem. If that does you can refactor it into a sturdier solution later.
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
Hope this will help you..
WebElement searchBox = driver.findElement(By.id("search-input"));
searchBox.sendKeys("somepostcode");
Actions actions = new Actions(driver);
actions.moveToElement(searchBox);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='address-0']/span")));
WebElement address = driver.findElement(By.xpath(".//*[#id='address-0']/span"));
actions.moveToElement(address);
actions.click();
actions.perform();
I have solved this problem by breaking the postcode in two parts
searchBox.sendKeys("postcodePart1");
searchBox.sendKeys("postcodePart2");
There must be kind of on change event was calling.

Selenium class name not found from the example

I found this selenium automation code on stackoverflow.
I was trying to run the program. It gives an error:
Unable to locate element: {"method":"class name","selector":"gssb_e"}
I inspected some web elements on that page to see if I will find the class name gssb_e. But I did not find any. I want to modify or update the class name but I am not sure what webelement the example is trying access. can you please help.
WebDriver driver = new FirefoxDriver();
// Go to the Google Suggest home page
driver.get("http://www.google.com/webhp?complete=1&hl=en");
// Enter the query string "Cheese"
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("Cheese");
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
WebElement resultsDiv = driver.findElement(By.className("gssb_e"));
// If results have been returned, the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {
break;
}
}
TL;DR: Use better location strategies and explicit waits.
I don't see an element with gssb_e class name when I do the same manually.
Instead, in this intentionally created "mess", I would try sticking to the more reliable things. For instance, if we are trying to wait until the results would appear, I would, for example, explicitly wait for the element with id="search" to become visible:
WebDriverWait wait = new WebDriverWait(driver, timeOut);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("search")));
In other words, comparing to the gssb_e class name that you were using, there are so many more reliable, more logical and simpler things on the Google Search Results page that can tell you the results are ready and visible.

Categories

Resources