I'm trying to move to a different page (click 'my account'), and a floating advertisement appears:
advertisement
I tried to click on it, and can't find the "Close" element.
Found that it might related to frames, but still not works.
My Code:
public void clickMyAccount() {
driver.findElement(myAccountBtn).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void clickCloseAd(){
driver.switchTo().frame(driver.findElement(By.id("google_esf")));
driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
driver.findElement(By.id("dismiss-button")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.switchTo().defaultContent();
}
Site:
https://practice.automationtesting.in/
Any idea how can I click the floating advertisement?
Tried: move between frames until able to find the Close element
Actual: still can't find this element
It is hard to handle advertisement popUp. So we can handle it in 2 ways:
downloading ad-blocker extension to your chrome and using the chrome option code.
download ad-bocker- https://chrome.google.com/webstore/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom
use chromeoption driver code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars");
Notice that popUp gets disabled is do back. so we can is click on myaccount go back and then click back:
WebElement MyAccount = driver.findElement(By.xpath("//[#href='https://practice.automationtesting.in/my-account/']"));
MyAccount.click();
driver.navigate().back();
MyAccount.click();
Found the issue:
My 'wait' not always working, so the driver tries to close the ad when not displayed - still searching for a way to correct this
It's working without frame1: "google_esf", only frame2 and frame3 needed
Updated code:
public void clickMyAccount() {
System.out.println("Click My Account");
if(driver.findElement(myAccountBtn).isDisplayed()){
driver.findElement(myAccountBtn).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
} else {
System.out.println("My account button not displayed");
}
}
public void clickCloseAd(){
System.out.println("Click Close Ad");
if(driver.findElement(By.id("aswift_9")).isDisplayed()){
// driver.switchTo().frame(driver.findElement(By.id("google_esf"))); //Not in use but also a frame
driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
driver.findElement(By.id("dismiss-button")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.switchTo().defaultContent();
} else {
System.out.println("Ad not displayed");
}
}
Thanks all!
Related
Trying to send data in table element (cell). Verified ID, Xpath, CssSelector and none them is wrong.Even, put timeout till page load. Even verified the iFrame (Already switchTo current iFrame).
No such element found error pops up every time. Not sure if I need to switchTo iFrame again after page load?
I tried all the possible ways but not pass through. I really appreciate any suggestions or new direction to think.
Thank you in advance.
Run my script for better insight where its failing.
public class SapDijon
{
WebDriver driver;
JavascriptExecutor jse;
public static void main(String[] args) throws Exception
{
SapDijon sapObj = new SapDijon();
sapObj.invokeBrowser();
sapObj.initializeSapDijon();
sapObj.ForecastME59();
}//End of Main
public void invokeBrowser()
{
System.setProperty("webdriver.chrome.driver", "U:\\Research Paper\\Selenium\\Drivers\\Chrome\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
}
public void initializeSapDijon() throws Exception
{
Thread.sleep(1200);
driver.get("http://dijon.cob.csuchico.edu:8041/sap/bc/gui/sap/its/webgui/?");
driver.findElement(By.id("sap-user")).sendKeys("H5");
Thread.sleep(1200);
driver.findElement(By.id("sap-password")).sendKeys("Onsjhjsa1087");
Thread.sleep(1200);
driver.findElement(By.id("sap-client")).clear();
Thread.sleep(1200);
driver.findElement(By.id("sap-client")).sendKeys("485");
Thread.sleep(1200);
driver.findElement(By.id("LOGON_BUTTON")).click();
}
public void ForecastME59() throws InterruptedException
{
driver.switchTo().frame("ITSFRAME1");
Thread.sleep(800);
driver.findElement(By.xpath("//td[#id='tree#105#4#1']//span[#class='lsSTStatusImg lsMirrorRtl lsSTStatusIcon urSTExpClo urCursorClickable']")).click();
Thread.sleep(800);
Actions action = new Actions(driver);
WebElement md61 = driver.findElement(By.xpath("//span[#id='tree#105#6#1#1#i']"));
action.doubleClick(md61).perform();
driver.findElement(By.id("M0:46:::4:2-imgSymb")).click();
driver.findElement(By.id("M0:46:::4:26")).sendKeys("HH-F");
driver.findElement(By.id("M0:50::btn[0]")).click();
Thread.sleep(6000);
driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[#id='tbl5732[1,8]_c-r']/input[#id='tbl5732[1,8]_c']")).click();
driver.findElement(By.xpath("//span[#id='tbl5732[1,8]_c-r']/input[#id='tbl5732[1,8]_c']")).sendKeys("100");
}
}//End of Class
After " action.doubleClick(md61).perform()" switch back from frame to default content and again switch to the available iframe.
To move back to the parent frame, you can either use switchTo().parentFrame() or if you want to get back to the main (or most parent) frame, you can use switchTo().defaultContent();
The issue persists because of a new window opened and the script was unable to find the element. The solution is to switch to a newly opened window to find element locator.
ArrayList tabs = new ArrayList (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
I am testing a native iOS mobile app using selenium and appium with Java code. As part of my teardown, I have to keep on clicking "back" button until "setting" button appears from where I can logout of the application.
I tried few things using do while but not working. Can anyone help please ?
Try this code may be help you
try {
boolean flag = true;
while(flag) {
WebElement backBtn = driver.findElementByName("back");
backBtn.click();
Thread.sleep(1000);
boolean isFindSettingBtn = driver.findElementsByName("setting").size() !=0;
if(isFindSettingBtn) {
break;
}
}
}catch(Exception e) {
e.printStackTrace();
}
I know there are several threads regarding this topic, but I am not necessarily looking for a solution, but rather an explanation. I work on a very large automation suite that tests a web application via mobile phones using browserstack. My stability is very low.. and it is due to this error getting thrown at me! Occasionally it will work and occasionally it will not.. I can not use Actions because Browserstack does not support that.. WHY does this error exist and has anyone had any success it working around it. I always wait for an object using wait.until(ExpectedConditions), but sometimes this does not work well enough. I cant quite catch it as an exception since it is an Unknown error. Also, our standards do not allow for a Thread.sleep(). Any ideas? Thank you so much
And here is a screen of some code..
You are waiting for a WebElement to be clickable, then again you are finding a list of WebElements and clicking the first element.
This does not guarantee that you are clicking the element you waited for it be clickable.
public void waitAndClickElement(WebElement element) {
driverWait.until(ExpectedConditions.elementToBeClickable(element)).click();
}
In your case,
public void clickImageView() {
driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click() ;
}
Element is normally not click able due to following reasons .
Html is loading and client is still receiving updates from server
When Scrolling
It can be due to some object is overlapping target
problem 3 can not be resolved you need to fix your code in this wait i wait for HTML to ready and then verify is it click able or not this has eliminated such exceptions from my code
how ever i made a solution for problem 1 and 2 you can simply use my custom wait before clicking . call this function
public static void waitForElementPresent(final By by, int timeout,WebDriver driver)
After this if you are using browser other then chrome then call Scroll to that object this would fix you problem
Code
public static void waitForElementPresent(final By by, int timeout,WebDriver driver) {
waitForPageLoad(driver);
WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,40).ignoring(StaleElementReferenceException.class);
/* wait.until(new ExpectedCondition<Boolean>(){
#Override
public Boolean apply(WebDriver webDriver) {
WebElement element = webDriver.findElement(by);
return element != null && element.isDisplayed();
}
}); */
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wait.until(ExpectedConditions.presenceOfElementLocated(by));
wait.until(ExpectedConditions.elementToBeClickable(by));
WebDriverWait wait2 = new WebDriverWait(driver, 40);
wait2.until(ExpectedConditions.elementToBeClickable(by));
}
//wait for page to laod
public static void waitForPageLoad(WebDriver driver) {
ExpectedCondition<Boolean> pageLoadCondition = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(pageLoadCondition);
}
This is due to the speed at which selenium runs. It will try and find elements before the page has loaded, thus resulting in this error.
For the code sample you provided, the .until() returns the WebElement you are waiting for. You can use the code below to click it rather than scraping the page again.
public void clickImageView()
{
driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click();
}
I am unable to Handle the Print window/popup when I click on a Print button on the Web application. I need to be able to either Close this Window, Click on Print or Cancel. I am not sure whether this is a pop-up or a window.
Could some one help me?
see whether any web elements are visible if you hover the mouse over the popup.
if web elements are visible then its web application pop up
if no web elements are visible then its windows popup
However you can ignore the popup by sending escape key. the following code will work. i just tried, it worked.
public class demo extends parent {
WebDriver driver = new FirefoxDriver();
#Test
public void launch() throws InterruptedException {
driver.get("https://www.google.co.in");
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement el = driver.findElement(By.xpath(".//*[#id='hplogo']"));
el.click();
Thread.sleep(10000);
el.sendKeys(Keys.CONTROL + "p"); // trying to invoke print pop up
Thread.sleep(10000);
r.keyPress(KeyEvent.VK_ESCAPE); //dismissing it by sending escape keys
}
}
hope you will get some idea here :)
Consider the below code:
getCurrentUrl(driver);
driver.close();
getCurrentUrl(driver);
public void getCurrentUrl (WebDriver driver) {
if (driver window is not closed)
System.out.println(driver.getCurrentUrl());
else
System.out.println("Window is not available");
}
Please let me know how to perform the "driver window is not closed" check in Java.
hmmm ... Consider this just as a guess. But I feel like that when you call driver variable after you close it, you can get NullPointerException because it will be null
But anyway. I would implement it like this:
public void getCurrentUrl (WebDriver driver) {
try {
System.out.println(driver.getCurrentUrl());
} catch (Exception e) { // the most top one
System.out.println("Window is not available");
}
}
There is possibly better and cleaner way of doing it. But I am not Java programmer and my gut feeling tells me this will work