In my project, there has one advertisement provider who shows us pop up advertisement but Problem is this pop-up will come it's not fixed.Sometimes add not show sometimes comes this pop up after 1 minute and interrupts my test case.
I write a code for close this popup but it's not a perfect solution I think..If anyone can help?
boolean Imclose = wd.findElement(By.xpath(".//*[#class='IM_overlay_close_container IM_overlay_close_button']")).isDisplayed();
if (Imclose == true) {
wd.findElement(By.xpath(".//*[#class='IM_overlay_close_container IM_overlay_close_button']")).click();
}
Try these code, to handle the unwanted pop up page. I have provided 180 seconds of wait. Then Click on close button inside the advertisement pop up page.
After click on advertisement pop up page, another window is getting open, So I have to redirect to my main window, then only further code will execute.
driver.get("https://www.rentbyowner.com/usa");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
try
{
WebDriverWait wait = new WebDriverWait(driver, 180);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[contains(#class,'IM_overlay_foreground')]"))));
driver.findElement(By.xpath("//div[contains(#class,'IM_overlay_foreground')]//span[#class='IM_close_text']")).click();
try
{
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
//driver.switchTo().window(winHandle).close();
}
driver.switchTo().window(winHandleBefore);
//Verify purpose written code, weather my driver is getting move to parent window or not.
WebDriverWait element = new WebDriverWait(driver, 10);
element.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("top_k_search"))));
driver.findElement(By.id("top_k_search")).clear();
System.out.println("Try block");
}
catch(Exception a)
{
System.out.println("Inner Catch");
}
}
catch(Exception e)
{
System.out.println("Element not present");
//Provide your code here..
}
I observed that this is a normal pop-up overlay over the webpage and not an alert and it appears almost within 60 seconds. So, the below code might just work for you.
//Waiting for the Popup to appear
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'IM_overlay_foreground')]")));
//Clicking on the 'Close' text to close the popup.
driver.findElement(By.xpath("//div[contains(#class,'IM_overlay_foreground')]//span[#class='IM_close_text']")).click();
NOTE: You can increase the time from 60 to 90, in case it appears to take longer. Thing is, whenever the popup appears within the timeframe set, it is handled(as in Closed).
Actually it is coming after 30-50 seconds so you have to wait and then click on close button.
System.setProperty("webdriver.chrome.driver", "E:\\software and tools\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.rentbyowner.com/usa");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
WebElement ad=driver.findElement(By.xpath(".//*[#id='IM_target_overlay']/div/div/div/div[1]/a/div"));
WebDriverWait wait= new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(ad));
ad.click();
Refer this link for more information:
Handling Alerts in Selenium Webdriver
Related
I am trying to make some tests using selenium based Katalon Studio. In one of my tests I have to write inside a textarea. The problem is that I get the following error:
...Element MyElement is not clickable at point (x, y)... Other element would receive the click...
In fact my element is place inside some other diva that might hide it but how can I make the click event hit my textarea?
Element ... is not clickable at point (x, y). Other element would receive the click" can be caused for different factors. You can address them by either of the following procedures:
Element not getting clicked due to JavaScript or AJAX calls present
Try to use Actions Class:
WebElement element = driver.findElement(By.id("id1"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
Element not getting clicked as it is not within Viewport
Try to use JavascriptExecutor to bring the element within Viewport:
JavascriptExecutor jse1 = (JavascriptExecutor)driver;
jse1.executeScript("scroll(250, 0)"); // if the element is on top.
jse1.executeScript("scroll(0, 250)"); // if the element is at bottom.
Or
WebElement myelement = driver.findElement(By.id("id1"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement);
The page is getting refreshed before the element gets clickable.
In this case induce some wait.
Element is present in the DOM but not clickable.
In this case add some ExplicitWait for the element to be clickable.
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("id1")));
Element is present but having temporary Overlay.
In this case induce ExplicitWait with ExpectedConditions set to invisibilityOfElementLocated for the Overlay to be invisible.
WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
Element is present but having permanent Overlay.
Use JavascriptExecutor to send the click directly on the element.
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
I assume, you've checked already that there is no any other component overlapping here (transparent advertisement-iframes or some other component of the DOM => seen quite often such things in input/textfield elements) and, when manually (slowly) stepping your code, it's working smoothly, then ajax calls might cause this behaviour.
To avoid thread.sleep, try sticking with EventFiringWebDriver and register a handle to it.
(Depending on your application's techstack you may work it for Angular, JQuery or wicket in the handler, thus requiring different implementations)
(Btw: This approach also got me rid of "StaleElementException" stuff lots of times)
see:
org.openqa.selenium.support.events.EventFiringWebDriver
org.openqa.selenium.support.events.WebDriverEventListener
driveme = new ChromeDriver();
driver = new EventFiringWebDriver(driveme);
ActivityCapture handle=new ActivityCapture();
driver.register(handle);
=> ActivityCapture implements WebDriverEventListener
e.g. javascriptExecutor to deal with Ajax calls in a wicket/dojo techstack
#Override
public void beforeClickOn(WebElement arg0, WebDriver event1) {
try {
System.out.println("After click "+arg0.toString());
//System.out.println("Start afterClickOn - timestamp: System.currentTimeMillis(): " + System.currentTimeMillis());
JavascriptExecutor executor = (JavascriptExecutor) event1;
StringBuffer javaScript = new StringBuffer();
javaScript.append("for (var c in Wicket.channelManager.channels) {");
javaScript.append(" if (Wicket.channelManager.channels[c].busy) {");
javaScript.append(" return true;");
javaScript.append(" }");
;
;
;
javaScript.append("}");
javaScript.append("return false;");
//Boolean result = (Boolean) executor.executeScript(javaScript.toString());
WebDriverWait wait = new WebDriverWait(event1, 20);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return !(Boolean) executor.executeScript(javaScript.toString());
}
});
//System.out.println("End afterClickOn - timestamp: System.currentTimeMillis(): " + System.currentTimeMillis());
} catch (Exception ex) {
//ex.printStackTrace();
}
}
As #DebanjanB said, your button (or another element) could be temporarily covered by another element, but you can wait and click it even if you don't know which element is covering the button.
To do this, you can define your own ExpectedCondition with the 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 this:
WebDriverWait wait10 = new WebDriverWait(driver, 10);
wait10.until(elementToBeClickable(btn));
wait10.until(new SuccessfulClick(btn));
Try Thread.Sleep()
Implicit - Thread.Sleep()
So this isn’t actually a feature of Selenium WebDriver, it’s a common feature in most programming languages though.
But none of that matter.
Thread.Sleep() does exactly what you think it does, it’s sleeps the thread. So when your program runs, in the majority of your cases that program will be some automated checks, they are running on a thread.
So when we call Thread.Sleep we are instructing our program to do absolutely nothing for a period of time, just sleep.
It doesn’t matter what our application under test is up to, we don’t care, our checks are having a nap time!
Depressingly though, it’s fairly common to see a few instances of Thread.Sleep() in Selenium WebDriver GUI check frameworks.
What tends to happen is a script will be failing or failing sporadically, and someone runs it locally and realises there is a race, that sometimes WedDriver is losing. It could be that an application sometimes takes longer to load, perhaps when it has more data, so to fix it they tell WebDriver to take a nap, to ensure that the application is loaded before the check continues.
Thread.sleep(5000);
The value provided is in milliseconds, so this code would sleep the check for 5 seconds.
I was having this problem, because I had clicked into a menu option that expanded, changing the size of the scrollable area, and the position of the other items. So I just had my program click back on the next level up of the menu, then forward again, to the level of the menu I was trying to access. It put the menu back to the original positioning so this "click intercepted" error would no longer happen.
The error didn't happen every time I clicked an expandable menu, only when the expandable menu option was already all the way at the bottom of its scrollable area.
I am trying to automate a website using selenium, the value is entering fine but when clicking to button without showing any response in the website the program is getting terminated what should be the reason?
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("somewebsite.html");
driver.findElement(By.xpath("//*[#id='abc']")).sendKeys("0000");
driver.findElement(By.xpath("//*[#id='xyz']")).sendKeys("5020");
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[contains(text(), 'Click
Me')]")).click();
//after clicking this button website is not showing any responce and
the program terminates successfully
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*
[#id='pqr']")
It is best to use explicit waits instead of implicit waits. Explicit waits wait for your given condition and when the condition is fulfilled it stops waiting.
Since you are waiting for something to be present after the click, wait for that element to be visible using explicit wait.
Try this:
// declare a wait first and you could reuse it
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement button = driver.findElement(By.xpath("//span[contains(text(), 'Click Me')]"));
// instead of implicit wait this waits for the button to be clicable and doesn't wait more than necessary
wait.until(ExpectedConditions.elementToBeClickable(button));
button.click();
WebElement expected_element = driver.findElement(By.id("pqr"));
//this waits for your expected element to be visible after click. if the element is not visible after 30 seconds it will throw timeout exception
wait.until(ExpectedConditions.visibilityOf(expected_element));
We are working on IE Automation using Selenium Web driver in C#.Net.
We are getting an exception in handling model popup window. We supposed to do below the action.
When we click on Link button it will open a popup window then we need switch to popup window selecting check box options and click on Submit button.
When clicking on Link button we are able to open the popup window. But here we are facing an issue like the child popup window is not loading with data and getting HTTP 500 Internal server Error.
I don't understand sometimes it was working properly with the same code but not all the times I am getting above issue when I am trying to perform above actions on child window.
is this any IE settings issue or my code issue even i ignored protected mode settings in IE settings.
I am trying with below code :
js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[#id='ByNewNotes']")));
(or)
string jsWindowString = "NewWindow('pop_Type.jsp?Type=External&IuserId=NUVJK50'," + sessionId + ",'400','500');return false";
((IJavaScriptExecutor)driver).ExecuteScript(jsWindowString);
Could you please help on this issue.
Thanks in Advance.
Instead of using ExpectedConditions.ElementExists use ExpectedConditions.elementToBeClickable or presenceOfElementLocated
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""//*[#id='ByNewNotes']")));
element.click();
Either Try to use FluentWait. Create a by function of your element which you want to wait and pass it in below method
WebElement waitsss(WebDriver driver, By elementIdentifier){
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
return wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(elementIdentifier);
}});
}
Hope it will help you :)
Have you tried
Thread.Sleep(2000);
We had the same issues and solved it with this simple way.
After clicking save button a pop will open and i write a code below to do accept but its not working.
`driver.findElement(By.id("save")).click();
Alert succ=driver.switchTo().alert();
System.out.println(succ.getText());
Thread.sleep(2000);
succ.accept();`
Need Help please.
The behavior of selenium varies according to browser, In some case the actions done in Firefox browser like form filling actions or clicking the button is faster then Chrome browser so your script well executed in chrome but throw error in Firefox so you need to add some pause to make them perform well.
So in your case after clicking the save button selenium executing the command too fast so skip to switch on alert and accept so add some wait using Thread.sleep(); to make pause
driver.findElement(By.id("save")).click();
Thread.sleep(2000);
Alert succ=driver.switchTo().alert();
System.out.println(succ.getText());
Thread.sleep(2000);
succ.accept();
Note : It is not recommended to use Thread.sleep(); instead use implicit or explicitwait conditions
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.alertIsPresent());
Try this:
driver.findElement(By.id("save")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(driver.switchTo().alert().getText());
driver.switchTo().alert().accept();
Is it possible to execute a command while waiting for an element to appear? In particular I'm waiting for an element to appear but it's not going to appear unless I refresh the page. Here's my code
wait = new WebDriverWait(driver, 30);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_here")));
state = element.getText();
The element I'm waiting for is never going to appear unless I hit the app's refresh button. How can I refresh during the 30 second timeout that I set?
You can do something like this:
System.out.println("before wait");
WebElement el = (new WebDriverWait(driver, 30))
.until(new ExpectedCondition<WebElement>(){
//try the following cycle for 30 seconds
#Override
public WebElement apply(WebDriver driver) {
//refresh the page
driver.navigate().refresh();
System.out.println("Refreshed");
//look for element for 5 seconds
WebElement sameElement = null;
try{
sameElement = (new WebDriverWait(driver, 5))
.until(new ExpectedCondition<WebElement>(){
#Override
public WebElement apply(WebDriver driver) {
System.out.println("Looking for the element");
return driver.findElement(By.id("theElementYouAreLookingFor"));
}});
} catch (TimeoutException e){
// if NULL is returns the cycle starts again
return sameElement;
}
return sameElement;
}});
System.out.println("Done");
It will try to get the element for 30 seconds, refreshing the page every 5 seconds.
I would add more lines to that code: basically, if state.isEmpty() then use a Webdriver "Action" class to move the mouse to the refresh button (you app would need a refresh button inside of it for this to work) then run the wait one more time to verify it appears.