Tried executing the code but getting:
ElementNotInteractableException: element not interactable
for the below sendKeys statement. What could be the solution??
WebElement ss = driver.findElement(By.cssSelector("div[class='select__single-value css-1uccc91-singleValue']"));
ss.click();
ss.sendKeys("abc");
Generally <div> tags are not interactable unless [contenteditable="true"] (How to modify the innerHTML of a contenteditable element) is attribute is explicitly set.
Solution
If your usecase is to invoke click() / sendKeys() generally you have to invoke on <input> elements but not on <div>. Try to identify the element using the inspector opening the google-chrome-devtools and identify the desired <input> element.
References
You can find a couple of relevant detailed discussion on ElementNotInteractableException in:
Selenium WebDriver throws Exception in thread “main” org.openqa.selenium.ElementNotInteractableException
How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver?
Please make sure you are using the correct path as div tags are usually not interactable.
By.cssSelector("div[class='select__single-value css-1uccc91-singleValue']");
Here, you are using the compound class which is no longer allowed in selenium.
So, try using:
By.cssSelector("div[class='.select__single-value.css-1uccc91-singleValue']");
Related
snapshot
<button class="add-school btnEditDashboard btn btn-primary pull-right" ng-click="CreateNewSchool()" name="AddSchool" type="submit">Add School</button>
Tried xpath also. (\\html/body/div[2]/div/div[2]/div/div/div[1]/div/div[2]/button)
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"tag name","selector":"AddSchool"}
try with this code :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Add School' and #name='AddSchool' and #ng-click='CreateNewSchool()']"))).click();
If it is in iframe , please switch focus of driver to respective iframe and then try to interact with it.
AddSchool is the name attribute, not the tag name (which will be button in your case). Try
findElement(By.name("AddSchool"))
To locate the element instead of using an absolute xpath it would be optimum to use a relative xpath. Now, as per the HTML you have shared the element is an Angular element so you have to induce WebDriverWait for the element to be clickable as follows:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='add-school btnEditDashboard btn btn-primary pull-right' and #name='AddSchool']"))).click();
Since the HTML is not shared, it could be 2 things
Iframe: You need to change your drivers focus to the iframe the element is in.
https://www.guru99.com/handling-iframes-selenium.html
Selenium is executing before the element is visible. Use Explicit Wait to wait for the element to be interactable before clicking it. Understand all the type of wait conditions through http://www.seleniumeasy.com/selenium-tutorials/webdriver-wait-examples
Try this xpath:
//button[#ng-click='CreateNewSchool()']
I am trying to execute below Selenium Web driver script, But I am getting
org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible
<span class="button-inner">Login</span>
#Test
public void BrowserInvocation() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\DOGETHER\\Desktop\\Website\\chromedriver.exe");
#SuppressWarnings("unused")
ChromeDriver driver=new ChromeDriver();
driver.get("http://phasorlab-web-dev.s3-website-us-east-1.amazonaws.com/"); /*Get URL */
driver.manage().timeouts().implicitlyWait(65, TimeUnit.SECONDS);
driver.findElementByXPath("//span[#class='button-inner']").click();
//driver.findElementByClassName("button-inner").click();
//driver.findElementByTagName("[text()='Login']").click();
There are two problems with your locators.
Your XPATH //span[#class='button-inner'] identifies two nodes on the DOM. The first node is hidden and hence the exception ElementNotVisibleException. Same is case with driver.findElementByClassName("button-inner")
findElementByTagName("[text()='Login']") is incorrect because findElementByTagName only needs the tag name and is mostly used when you want to get a list of elements of a particular tag. For example, driver.findElementByTagName("a") would give you a list of all the links on the page
Solution:
Using the XPATH //span[#class='button-inner' and text()='Login'] uniquely identifies the right element.
Or simply //span[text()='Login']
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Login']")
You could use explicit wait for handling the exception, e.g. if WebDriver instance is driver.
WebDriverWait wait=new WebDriverWait(driver,//mention the time);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(//mention the By locator));
Refer following link to see available expected conditions for explicit wait.
The text login is in uppercase "LOGIN".
so try this:-
//span[text()='LOGIN']
The right way is to use the ID of the element because there are higher chances of text to get change.
click here to check the page I am working on
The following is the java code:
driver.findElement(By.xpath(".//*[#id='ref_2665398031']/li[4]/a/span1")).click();
The element is actually in the left navigation pane.
Here when this particular statement is getting executed, I can see that browser is moving down, but it does not click that element.
You have not properly written the xpath:
".//*[#id='ref_2665398031']/li[4]/a/span1"
it should be : "//[#id='ref_2665398031']/li[4]/a/span[1]"
and if using WebDriver then
"//[#id=\"ref_2665398031\"]/li[4]/a/span[1]"
After correcting above if still error exists:
You are using a relative path which is relative to the element having id='ref_2665398031.
This id seems to be generated dynamically.
Please confirm two things:
Your code is not giving element not found error.
Page refresh is not changing this id. (If it is being generated dynamically than it will change on page refresh.)
Otherwise try using different approach:
driver.findElement(By.xpath("//*[contains(text(), '50% Off or more')]"));
Use this xpath:
driver.findElement(By.xpath("//ul[#id='ref_2665398031']/li[4]/a/span[1]")).click();
Try Below xpath it is working for me
driver.findElement(By.xpath("//ul[contains(#id,'ref_2665398031')]/li[4]/a")).click();
Try JavaScriptExecutor instead of using normal Selenium click method.
WebElement element = driver.findElement(By.xpath(".//span[contains(text(),'50% Off or more')]"));
((JavascriptExecutor) driver).executeScript("return arguments[0].click();", element);
Does anyone know how I can click (in Java) the button with following HTML code?
<div role="button" id=":t5.ss" class="c-N-K a-b a-b-va KMD69e-bU2Jkc-b DF" tabindex="0" aria-label="Join as John" style="user-select: none;">Join</div>
My snippet code in Java:
driver.get("https://www.somepage.com");
... enter new tab ...
Thread.sleep(10000);
driver.findElement(By.xpath("//div[#role='button']")).click();
And I've got
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element
{"method":"xpath","selector":"//div[#role='button']"}
I've tried also following without success:
driver.findElement(By.xpath("//div[#id=':t5.ss']")).click();
driver.findElement(By.xpath("//div[#aria-label='Join as John']")).click();
driver.findElement(By.cssSelector("div[id=':t5.ss']")).click();
Selenium has methods named By.id() and By.className() to find the button and use click() to click the selected button.
driver.findElement(By.id("element id")).click()
OR
driver.findElement(By.className("element class")).click()
To click an element, it has to be visible and displayed.
WebElement e = find(...); //POM search
if (e != null){
Thread.sleep(...);
while (!e.isDisplayed())
Thread.sleep(...);
e.click();
}
This has worked for me a lot.
Using Thread.sleep in your tests is bad practice because you will never know if the page and the elements you want to use actually are finished loading.
It's better to use explicit waits. Here's an example:
new WebDriverWait(driver, timeout).until(ExpectedConditions.visibilityOfElementLocated(by));
More information about that can be found here.
The NoSuchElementException points towards three possibilities:
a) there simply is no such element that you are trying to find in the HTML
b) the element isn't loaded yet, if so use WebDriverWait
c) the element is part of an iframe and cannot be found unless you switch to that iframe
If c) is correct you can switch to the iframe with:
driver.switchTo().frame(d().findElement(iframe));
Instead of click(), try sendKeys().
Example:
myDiv.sendKeys(Keys.Enter); // or Keys.Return
You should use id as it is unique to identify the element
driver.findElement(By.id(":t5.ss")).click();
Also check if the element is displayed and enabled by using isDisplayed() and isEnabled() to click.
I looked simillar questions at stackoverflow and didn't find anything that can help me.
I have following html code:
<span class="help-block ng-scope" translate="">Company</span>
How can i find this element (Java)? I tried following variants:
driver.findElement(By.cssSelector(".help-block.ng-scope"))
driver.findElement(By.xpath("//span[contains(#class, "Company")]"))
I even tried to just copy xpath from browser console in two ways:
First:
driver.findElement(By.xpath("//*[#id="ngCloudike"]/body/div[6]/div/div/div/div/form/div[1]/div[6]/span"))
Second:
driver.findElement(By.xpath("/html/body/div[6]/div/div/div/div/form/div[1]/div[6]/span"))
Also i tried to find By.ClassName, but i can't do this, because there are some spaces. So, how can i find element like this?
Try below expression:
driver.findElement(By.xpath("//span[text()='Company']"))
If you get NoSuchElementException with this code, try to add some time to wait until element present and visible:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Company']"))
If you get NoSuchElementException even with ExplicitWait, target element might be located inside an iframe. To handle element inside an iframe:
driver.switchTo().frame("iframeNameOrId");
driver.findElement(By.xpath("//span[text()='Company']"));