When I am trying to execute below code its giving me exception as:-org.openqa.selenium.ElementNotVisibleException: element not visible
WebElement elem = newDriver.findElement(By.name("loginId"));
elem.get(0).clear();
elem.get(0).sendKeys("asd");
even though element is present.
For more details see the below image.
I am trying to access input box below account label but its giving me exception as element is not visible.
I already used Actions tag and JavascriptExecutor
Any suggestions.
The interested element is inside an iframe:
So, before, you have to switch to the iframe:
WebElement iframe= driver.findElement(By.id("alibaba-login-box"));
driver.switchTo().frame(iframe);
If you want to go "out" the iframe:
driver.switchTo().defaultContent();
The entire code:
WebElement iframe= driver.findElement(By.id("alibaba-login-box"));
driver.switchTo().frame(iframe);
WebElement elem = driver.findElement(By.id("fm-login-id"));
elem.clear();
elem.sendKeys("asd");
//when you want to return to the defaultContent
driver.switchTo().defaultContent();
Related
I'm trying to do a simple selenium java automation for automating "sign in and sign out of amazon.com site". I'm able to sign in using element locator techniques, like XPath and CSS selector. But for signout, I'm thrown with ElementNotInteractable exception.
Below is the code that I tried(posting the code segment of signout alone).
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).click();
I have tried the above code segment with different element locator techniques like CSS selector and etc, but no luck.
Kindly suggest if I can find and click the sign-out link in the flyout menu by any other method.
Thanks.
You can try below code in which explicit wait is implemented so it will wait for the element to click
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element1 =driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-
belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
ele2=driver.findElement(By.xpath("//a[#id='nav-item-signout']"))
wait.until(ExpectedConditions.elementToBeClickable(ele2));
ele2.click();
Instead of the click() method try this :
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.sendKeys(Keys.RETURN);
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).sendKeys(Keys.RETURN);
And instead of RETURN you can also try ENTER
You can try below code in which mover hover is implemented so it will hover the menu then you can click for sign-out.
WebElement ele = driver.findElement(By.id("nav-link-accountList-nav-line-1"));
Actions action = new Actions(driver);
action.moveToElement(ele).perform();
driver.findElement(By.xpath("//*[#id='nav`enter code here`-item-signout']/span")).click();
I am trying to do web automation.
I am defining a pop-up menu containing a button defined with either xpath or css respectively as
XPath:-->: //button[contains(text(), 'Open Door')
CSS:-->: div.device-item.content.view-content > div.detail > div > button.btn.btn-primary.ng-star-inserted
While all is well, it throws
org.openqa.selenium.ElementClickInterceptedException: element click intercepted:
when I am debugging the test one step at a time, it
runs successfully by clicking the button, with out any
problem. But when I am running the test, it fails. I hope it is not a wait issue, as we apply check waiting for
the presence of the button and verify it exists and clickable.
I believe many would advice to use JavaScriptExecutor approach, but our framework has a problem of returning any web element as a custom object called "Element" which is neither Web Element nor sub class of it, but extends Object and implements an interface called IElement, so we can't use JavaScriptExecutor method since it needs Web Element form of the button which we want to click on.
If it works in debug it means the overlay disappear automatically. You can wait for it to vanish
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("[id^='device-card']")));
And in any case you can wait for the button to be clickable
button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(), 'Open Door')")));
button.click();
It will look something like:
driver.executeScript("document.querySelector('button.btn').click()")
Just adjust the css
There are a couple of things you need to take care:
XPath //button[contains(text(), 'Open Door'): This xpath can be optimized further in either of the formats:
//button[text()='Open Door']
//button[contains(., 'Open Door')]
CSS: div.device-item.content.view-content > div.detail > div > button.btn.btn-primary.ng-star-inserted looks good however I strongly believe button.btn.btn-primary.ng-star-inserted would also work.
"...Executes successfully in debug mode...": As the browser client gets ample time to settle the webpage dynamics and the desired WebElement gets enough time to get interactive.
"...we amply check waiting for the presence of the button and verify it exists and clickable...": Is a myth as:
presenceOfElementLocated(): Is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
where as:
elementToBeClickable(): Is the expectation for checking an element is visible and enabled such that you can click it.
As your usecase is to invoke click() on the element you need to use the ExpectedConditions as elementToBeClickable().
Example:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_element"))).click();
You can find a detailed discussion in What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver?
Finally, when using the Selenium java clients, of coarse there are methods from JavascriptExecutor but that should be the last resort.
Update
As you are still seeing the error ...ElementClickInterceptedException: element click intercepted..., you can add an additional step to induce WebDriverWait for the invisibility and you can use either of the following options:
invisibilityOf(WebElement element):
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(overlapping_webelement));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
invisibilityOfElementLocated(By locator):
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("css_overlapping_element")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
You can find a relevant detailed discussion in Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working
you can move the mouse to that element and then click on it, even I was facing this issue and this solution solved it
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(), 'Open Door')").click().build().perform;
this happens even when the element is not visible completely onscreen, in this case you can scroll to that element and then use the above code as shown below
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", ele);
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(), 'Open Door')").click().build().perform;
click element using javascriptExecutor
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
This even happens sometimes with the way we selected the element, the xpath can be changed, we can try to use a parent tag rather than the button and try
you can define your own ExpectedCondition with click action:
public class SuccessfulClick implements ExpectedCondition<Boolean> {
private WebElement element;
public SuccessfulClick(WebElement element) { //WebElement element
this.element = element;
}
#Override
public Boolean apply(WebDriver driver) {
try {
element.click();
return true;
} catch (ElementClickInterceptedException | StaleElementReferenceException | NoSuchElementException e) {
return false;
}
}
}
and then use it:
wait10.until(elementToBeClickable(btn));
wait10.until(new SuccessfulClick(btn));
I have the following element:
<input type="hidden" data-dojo-attach-point="vn" value="adrija" aria-
hidden="true">
The above element is an element of dropdown and is hidden. The code that I have written is:
private WebElement adrija = Driver.driver.findElement(By.xpath("//input[#value='adrija' and #data-dojo-attach-point='vn']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", adrija);
It says it's not able to find the element.
Please help. Thanks. :)
Use css selector
driver.findElement(By.cssSelector("input[type='hidden']"))
Or xpath
driver.findElement(By.xpath("//input[#type='hidden']"))
Note : the field has type hidden. You cannot do visible interaction like sendkeys or click as it is invisible
Selenium cannot work on the hidden elements. First, you need the click on the button which opens the drop-down menu. Then you do whatever you want. :).
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.xpath("XPATH"));
element.click();
WebElement childElement = driver.findElement(By.xpath("HidenElementXpath"));
childElement.click();
If the hidden element is in a frame, you need to switch to the frame!!!
Then try to find the element
I have a piece of selenium code to look for a certain element and afterwards to click on this same element. The element is found, but after that, the click does not seem to do the trick. Here is the code for finding the element (unfortunately using xpaths, because there are no id's and it uses a lot of selfmade methods):
String taakButton = "Start taak button";
String xpathButton = "//td[contains(text(),'" + datumhelper.formateerDatumVoorFlowAanmaakdatum(SoapHelper.datumVoorAanvraag5SpaarrekeningSns) + "')]/following-sibling::*[4]/button";
WebElement startButton = selenium.searchForElementByXpathWithoutSwitchToFrame(xpathButton, taakButton);
I use the above String xpathButton to store a WebElement in a variable, after that I pass the WebElement to a method in a different class:
The searchForElementByXpathWithoutSwitchToFrame looks for the element and verifies if it is found.
WebElement startTaakButton = selenium.searchForElementByXpathWithoutSwitchToFrame(xpathStartTaakButton, taakButton);
The element is found, now click on it :
selenium.klikStartOfInzienTaak(startTaakButton, xpathStartTaakButton);
The klikStartOfInzienTaak method, which performs the click looks like this:
public void klikStartOfInzienTaak(WebElement webElementToBeClicked, String xpathToBeClicked) throws InterruptedException {
Actions action = new Actions(driver);
//check to see if element is not null
Assert.assertNotNull("WebElement 'startOfInzienTaak' niet gevonden", webElementToBeClicked);
//Thread.sleep(2500);
//I use Action doubleClick in the hope that would work.
action.moveToElement(webElementToBeClicked).doubleClick().build().perform();
I also used regular driver.click(). It seems the element is found because it does not give a NoSuchElementException and I see the focus is on the element to be clicked, but nothing happens:
Button to be clicked
When I uncomment the Thread.sleep it works, but I don't want to be using Thread.sleep.
As you can see in the image below with the loading image, the page seems to be (re)loading again after the element was already found and clicked on. Thats why the Thread.sleep works:
Button found and clicked but page (re)loading
Does anybody know what to do in order for me to remove the thread.sleep? Selenium has to somehow wait again for the page to reload again although the element is already found?
Try JavascriptExecutor to click the element,
Refer code,
WebElement element = driver.findElement(By.xpath(xpathButton));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
You can use WebDriverWait function to first find the element in the webpage and then can click on the element. By this i didn't had to use thread.sleep.
WebDriverWait for_element = new WebDriverWait(20, TimeUnit.SECONDS);
for_element.until(ExpectedConditions.elementToBeClickable(driver.findElement(by what means u have to find the path);
dr.findElement().click();
Hope that this works for you as it worked for me!
i successfully send text to ckeditor through these command
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Automation Description");
Problem
Text "Automation Description" is succesfully passed to ckeditor.
But. it does not locate the next element
Following error is displayed
no such element: Unable to locate element:
{"method":"xpath","selector":".//*[#id='select2-state_name-container']"}
When I added Block Comment to
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Automation Description");
It successful located the next element.
Help me to add a command so that I can locate next element after texting text to ckeditor. Thank you
Here's a screen shot:
As I seeing your code you are going to switch frame first then find ckeditor and set value. But you didn't switch back to default content to find the next element that's why selenium searching the locator into already switched frame where element is not present actually, So you should try to switch back before finding element as below :-
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Automation Description");
//Now switch back first to default
driver.switchTo().defaultContent();
//Now you can go to find next element
Hope it helps...:)
I believe this has to do with your switch frame statement,
driver.switchTo().frame()
Try to switch back to 0 index frame or default one after inputting the text:
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Automation Description");
driver.switchTo().frame(0); //To go back to main frame
//continue
Or better yet find the selector for the main frame window and swicth to it, but i think 0 should work in your case
Hope this helps