My Java Selenium WebDriver script clicks a link in a web page that opens a new tab and navigates to an external site. There are multiple of these links to different social media.
The problem I have is that sometimes the new tab opens and tries to load the external page but stops all together.
I figured this isn't a big deal, I will just refresh the page with my code like I would manually in the browser and it will attempt to reload the page. I was wrong.
Evidently, perhaps in Chrome only, the reload/refresh functionality included with Selenium and even JavaScript does not work the same as clicking the refresh button. In this circumstance, they do nothing at all.
Luckily, this only happens every now and then but it does cause my test to fail when it does happen.
When this happens, the title area of the tab says "untitled", the page is blank white with nothing on it, and the desired URL will be in the address bar. Only manually clicking the refresh button will reload the page properly.
I haven't seen this happen in Firefox even once so I am going to assume it is only an issue in Chrome.
Does anyone know how to work around this issue?
Here is what I have tried in JavaScript:
document.location.reload(false);
document.location.reload(true);
I tried these in Java Selenium:
driver.navigate().refresh();
public Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys(Keys.F5).keyUp(Keys.CONTROL).perform();
I should also say that these refresh methods work normally when the page is working normally. It is literally this one situation where they do not and I don't know what to do. Is there no way to programmatically click the refresh button in the browser? I need to find out if I can move the mouse and have it click.
Might be chrome web driver issue. try updating to latest.
driver = new ChromeDriver();
driver.manage().window().maximize();
baseUrl = "http://www.google.com/";
driver.get(baseUrl);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("https://www.facebook.com");
driver.switchTo().window(tabs.get(0)); // switch back to main screen
driver.get("https://www.linkedin.com");
Related
My flow is as follows in selenium:
Access a webpage
Click a tab
Click on add button in a tab where a window would open
Close that window
Click on that same tab again
I'm able to go through steps 1 to 4 without issues however at step 5 i'm not able to click the tab element knowing that i've clicked that same tab at step2, I did check from console and the same xpath I used in step2 did not return any element however when i clicked on that element to inspect it in console it started returning some values in console but still didn't work from selenium when i continued the run (in debug)
My page is in an Iframe which i was successfully able to access and print page title after step 4 but my issue remains, why aren't i able to click that tab from selenium after i close the window and the screen refreshes, why are the elements unresponsive anymore?
After doing some research i also tried the below script:
WebElement element=driver.findElement(By.xpath(the_path));
JavascriptExecutor ex=(JavascriptExecutor)driver;
ex.executeScript("arguments[0].click()", element);
but it would fail at the first line since the element is not fount, tried to initialize it earlier in the code but then it would fail at line 3.
How do i fix this issue?
Try to click on the element using Selenium code, not by injecting JavaScript to the pages. This should solve your problem.
WebElement element = driver.findElement(By.xpath(the_path));
element.click();
The issue was due to having multiple Iframes in the main page, so when closing switching windows I was not redirected to the proper Iframe in which i was in step 3 when clicking the tab.
I am trying to switch to a Gmail login popup that appears on clicking a button on my chrome. When I do a driver getWindowHandles(), it shows only one window - the parent. Also I noticed in the taskbar that the popup that opens has a google/gmail icon instead of chrome browser icon which is why I am assuming that the Webdriver does not count it as a second open "chrome" window, but instead a different one altogether.
So far I have tried everything on the driver methods
driver.switchTo().window(1 or 0)
Any suggestions would be helpful!!
driver.switchTo().alert().anyMethod
can be used.
First of all, why are you using driver. with Selenide? Selenide has a static WebDriver call - getWebDriver() (if really needed) with the next import import static com.codeborne.selenide.WebDriverRunner.getWebDriver;
From your quesiton, it is not clear what you want - either its pop up that appears after clicking or you want to switch to another chrome tab window?
If its a pop up alert, then the next code should help (to accept it for example):
getWebDriver().switchTo().alert().accept();
If you want to open a tab or focus on it:
switchTo().window(1);
I am using Actions to scroll to the bottom of the page:
public void scrollToBottomPage(){
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
}
but in next step, I have to click on the back button(available in my web application).
driver.findElement(By.cssSelector(".btn.terraVM-ActionToolbar-button--back")).click();
Selenium webdriver is opening unwanted right-click context menu and my script is getting failed.
Using chrome browser with latest selenium webdriver jar and chrome browser jar.
Working on mac OS.
I was not releasing CONTROL button in next:
public void scrollToBottomPage(){
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
actions.keyDown(Keys.CONTROL).release().perform();
}
Silly mistake.. :P
I am writing automation test scenario using selenium webdriver in java and cucumber.
Scenario:
2.1 I have two tabs(Parent tab and child tab). I will switch to child tab.
2.2 After switching to child tab and I will refresh a page. After that, I need to click an button in that page.
The problem, Which I am facing is I am unable to click that button element present in that page.
Note:- If I don't refresh a page means, I am able to click that button element in that page.
Even, I tried to get the current url, I am able to get the correct child tab url.
As per my observation, controlling is missing after giving the refresh page via automation.
Can anyone suggest the solution to handle control, after refreshing an web
Sample Code:
#Given("^I choose Segments menu$")
public void I_choose_Segments_menu() throws Exception {
getSegmentsCentralDSL().clickDmpSegmentsButton();
segmentCentralPage.getDmpSegmentGrid();
segmentCentralPage.refreshPage();
Thread.sleep(20000);
ArrayList tabs = new ArrayList(segmentCentralPage.getDriver()
.getWindowHandles());
System.out.println(tabs.size());
segmentCentralPage.getDriver().switchTo()
.window(tabs.get(1).toString());
String currentUrl = segmentCentralPage.getDriver().getCurrentUrl();
System.out.println("currentUrl=" + windowTitle);
}
When I enter an iframe (to provide Credit Card Details) using
driver.switchTo().frame(iframe);
It does its job correctly and filling in the form in the iframe and I switch out of the iframe using
WebElement ccInfo = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(".//*[#id='paymentech_form']/div/button"))));
ccInfo.click();
driver.switchTo().defaultContent();
However, the behavior of the function is inconsistent. The problem is when I am running the test in the background (minimized or hidden window) it always fails to click on the button mentioned above while the rest of the form is filled in. This problem does not prevail when I have the window open and selected.
Is there a possible work around for this?