WebDriver not detecting input textbox, throws ElementNotInteractableException: element not interactable - java

I've written some code to send input to a textbox as shown below :
However during execution, the webdriver returns ElementNotInteractableException even though it shows the input type is a textbox.
I've tried to send input using the following ways but have been unsuccessful, seeking advice, thanks!:
1)
driver.findElement(By.xpath("//input[#name='bkg_no']")).sendKeys("ABCDE9000333");
2)
String bkg = "ABCDE9000333"
WebElement bkgNo = driver.findElement(By.xpath("//input[#name='bkg_no']"));";
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value="+bkg+";", bkgNo);
3)
driver.findElement(By.name("bkg_no")).sendKeys("ABCDE9000333");
4)
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementByName('bkg_no').value='PKG900890300'");

To send a character sequence to the <input> you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("form[name='form'][onkeydown*='search'] div.wrap_search input.input[name='bkg_no']"))).sendKeys("ABCDE9000333");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//form[#name='form' and contains(#onkeydown, 'search')]//div[contains(#class, 'wrap_search')]//input[#class='input' and #name='bkg_no']"))).sendKeys("ABCDE9000333");

Related

How to click on svg element in Selenium Java

I have a problem with clicking on svg element in selenium Java.
HTML code:
I have tried next code examples but they are not helped.
Actions action = new Actions(webDriver);
action.click(webElement).build().perform();
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click;", webElement);
Actions action = new Actions(webDriver);
Thread.sleep(2000);
action.moveToElement(webElement).click().build().perform();
I did not get any exception or error. But can not click on SVG element
Which solutions are exist for it?
The desired element is a svg element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
selenium4 compatible code
Using cssSelector:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[placement='bottom'] svg"))).click();
Using xpath:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#placement='bottom']//*[name()='svg']"))).click();
References
You can find a couple of relevant detailed discussions in:
How to click on SVG elements using XPath and Selenium WebDriver through Java

Selenium can't locate ANY element

So I have a webscraper which first of all needs to get past the cookie banner of a given website. Normally I'd just locate the element by id or classname and be done with it, but on this site none of the elements can be located. I've tried/checked the following:
The element of interest is <div id="cookiescript_accept" tabindex="0" role="button" data-cs-i18n-text="[]">Alles accepteren</div>
The element is not part of an iframe
The element is not part of a shadow DOM
Using wait.until(ExpectedConditions.visibilityOfElementLocated hits the 15 seconds timeout
Using driver.executeScript("return document.getElementById('cookiescript_accept');"); doesn't work either
The parent element and parent of parent can also not be found
I'm still fairly new to Selenium and HTML so I must be missing something, please tell me if you know what that is
Code:
public void loadUrl(String url) {
System.out.println("\t\t- loadUrl " + url);
idle5000();
driver.get(url);
idle5000();
setWindowSize();
idle5000();
printFirefoxCPU();
scrollViewport();
idle5000();
printFirefoxCPU();
}
loadUrl("https://www.schoolplaten.com/");
prepRunnable.getDriver().findElement(By.id("cc-cookiescript_accept")).click();
// -> NoSuchElementException
WebDriverWait wait = new WebDriverWait(prepRunnable.getDriver(), 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("cookiescript_accept")));
// -> Timeout
WebElement elem = (WebElement) prepRunnable.getDriver().executeScript("return document.getElementById('cookiescript_accept');");
elem.click();
// -> elem is null
I could locate the web element with the below XPath
//*[name()='div' and #id='cookiescript_accept']
and could perform click on it with the help of below code :
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[name()='div' and #id='cookiescript_accept']"))).click();
To click() on the element ALLES ACCEPTEREN you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("div#cookiescript_accept")).click();
xpath:
driver.findElement(By.xpath("//div[#id='cookiescript_accept']")).click();
However, the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#cookiescript_accept"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='cookiescript_accept']"))).click();
Alternative
As an alternative you can also use the executeScript() as follows:
cssSelector:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#cookiescript_accept"))));
xpath:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='cookiescript_accept']"))));
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element

javascript error: a.tagName.toUpperCase is not a function error clicking on a button using Selenium and Java

I am using Selenium Webdriver with testng. On one of the WebElements I am trying to perform click on button action.
I am picking the locator "ID" and performing the below action.
WebDriver webDriver = this.getWebDriver();
webDriver.findElement(By.id("addTagBtn")).click();
and i am getting below error -
javascript error: a.tagName.toUpperCase is not a function error
HTML code looks like below
This error message...
javascript error: a.tagName.toUpperCase is not a function error
...implies that the WebDriver instance attempted to invoke click() on the desired element even before the element was completely rendered with in the DOM Tree.
The desired element is a Angular element. So to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
id:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("addTagBtn"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td > button#addTagBtn"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td/button[#id='addTagBtn' and text()='Add']"))).click();
It worked when i used Javascript code i.e
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("document.getElementById('addTagBtn').click();");
```

Not able to select a checkbox in Selenium Webdriver with java

I'm trying to select a checkbox but I'm not able to and didn't find any solution in other threads.
Here is my HTML code for drop down list:
code:
#FindBy(css="input.ng-valid.ng-dirty.ng-touched")
WebElement chrgAllocFee;
chrgAllocFee.click();
but it is not working where as other checkboxes are working when I define and use in same way. I found that the difference as compared to others is that here html code has additional line with
Please suggest how to define so that it can be recognised.
Error getting:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"input.ng-valid.ng-dirty.ng-touched"}
(Session info: chrome=69.0.3497.92)
The desired element is an Angular element so you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ng-valid.ng-dirty.ng-touched[name='ChargeAllocationFee']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='ng-valid ng-dirty ng-touched' and #name='ChargeAllocationFee']"))).click();
I found solution:
I used javascript for clicking that field and it works fine and here is the code:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", CamWizOR.chrgAllocFee);
CamWizOR.chrgAllocFee -> WebElement name
#FindBy(xpath="//input[#name='ChargeAllocationFee' and #type='checkbox']")
WebElement chrgAllocFee;

Selenium WebDriver throws Exception in thread "main" org.openqa.selenium.ElementNotInteractableException

Test Scenario: Trying to capture and test Gmail Login.
Current Output: Mozilla instance opens up. Username is entered but
the Password is not being entered by the WebDriver code.
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
FirefoxDriver varDriver=new FirefoxDriver();
varDriver.get("http://gmail.com");
WebElement webElem= varDriver.findElement(By.id("identifierId"));
webElem.sendKeys("error59878#gmail.com");
WebElement nextButton=varDriver.findElement(By.id("identifierNext"));
nextButton.click();
varDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement wePass=varDriver.findElement(By.cssSelector(".rFrNMe.P7gl3b.sdJrJc.Tyc9J"));
wePass.sendKeys("test1");
ElementNotInteractableException
As per the documentation, ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the DOM Tree, it is not in a state that can be interacted with.
Reasons & Solutions :
The reason for ElementNotInteractableException to occur can be numerous.
Temporary Overlay of other WebElement over the WebElement of our interest:
In this case, the direct solution would have been to induce ExplicitWait i.e. WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated as follows:
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();
A better solution will be to get a bit more granular and instead of using ExpectedCondition as invisibilityOfElementLocated we can use ExpectedCondition as elementToBeClickable as follows:
WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();
Permanent Overlay of other WebElement over the WebElement of our interest :
If the overlay is a permanent one in this case we have to cast the WebDriver instance as JavascriptExecutor and perform the click operation as follows:
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
Now addressing the error ElementNotInteractableException in this particular context we need to add ExplicitWait i.e. WebDriverWait as follows :
You need to induce a bit of wait for the Password field to be properly rendered in the HTML DOM. You can consider configuring an ExplicitWait for it. Here is the working code to login into Gmail using Mozilla Firefox:
System.setProperty("webdriver.gecko.driver","C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement email_phone = driver.findElement(By.xpath("//input[#id='identifierId']"));
email_phone.sendKeys("error59878#gmail.com");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[#name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("test1");
driver.findElement(By.id("passwordNext")).click();

Categories

Resources