Is there a possibility to refresh a certain frame in Selenium - java

I would like to refresh a certain IFrame using Selenium.
Only approach that came to my mind was to use a Key for an f5 on certain WebElement.
driver.switchTo().parentFrame();
driver.findElement(By.id("main")).sendKeys("\\uE035");
But it does reload entire page and this is not what i need.
There is and option to reload it using right mouse button on a chosen frame then you can pick "reload a frame" option but chrome does not provide shortcut for this one which makes it impossible to use SendKeys() on a certain WebElement.

It looks like you're ivoking the refresh on the parentElement rather than the actual iFrame element.
Try something like below. If you have multiple iframe elements, you'll need to use a different selector.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementsByTagName('iframe')[0].contentWindow.location.reload();");

Related

Is this correct way to use "switch()" method in selenium?

Aim: To count links in footer section of a webpage.
Instead of this:
WebElement footerdriver=driver.findElement(By.id("gf-BIG"));
system.out.println(footerdriver.findElement(By.tagName("a")).size());
I want to write like this:
WebElement footerdriver=driver.findElement(By.id("gf-BIG"));
driver.switch(footerdriver);
system.out.println(driver.findElement(By.tagName("a")).size());
Is this correct way to use "switch()" method in selenium ?
Unless I am mistaken, switch() is not a method belonging to the Selenium Webdriver. The method switchTo() is used to change your focus to a different window, tab or iframe.
If I am understanding your question you want to find an element that is descending from a different element. The way to do this is just find the element using the WebElement method findElement(By locator). E.g:
driver.get("https://www.wikipedia.org/");
WebElement eng = driver.findElement(By.xpath("//a[#id='js-link-box-en']"));
System.out.println(eng.findElement(By.tagName("span")).getText());
This will find a span element that descends from the element "eng".
Here is a runnable example of a test using the method.
driver.switchTo();
should only be used to either switch to
iframes/frame/frameset
newly windows/tabs
Alerts
defaultContents
therefore,
WebElement footerdriver=driver.findElement(By.id("gf-BIG"));
driver.switch(footerdriver);
this is wrong since you are saying that you want to switch to a web element.
Also in selenium it is switchTo() not switch.

Selenium Click method performs click slightly outside the element boundary

I have an application created in ReactJS which also contains some EXTJS/jsp iframes inside it. Now when I try to click some anchor tag element (Hyperlink) using selenium click method it wasn't working. After some investigations, I found that the click is performed slightly above the element boundary. The selenium is able to identify the element using xpath but is unable to click the link correctly. Also tried using the action but it didn't work. Using offset is not an option as it relies too much on the screens resolution.
Any help is highly appreciated.
maybe you can set the width and height of the page to become larger. Some elements overlaps if the page does not support smaller screens.
If your locator is inside the IFrame, you will need to do a switch to that IFrame and then perform the click action, and after you need to switch out from that IFrame:
driver.switchTo().frame("your_iframe_name");
driver.findElement(By.xpath("your_locator")).click();
After use one of these to switch back:
driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
Also, there is another way to click on a element located inside an IFrame, by executing a js script.
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath("your_locator"));

Element could not be scrolled into view (even though it is right there) Selenium

I am using Selenium to run tests on a page with multiple drop-down menus (specifically a pop-up page which allows you to select some options then close it). I am able to click on some of these menus totally fine; however, some of them throw an ElementNotInteractable exception with the message "element could not be scrolled into view", even though the menus are right beside each other. I am very confused as to why one menu works and the other does not even though they appear to be the same. The three things I have tried in order to click on the menu are:
a) Regular Selenium clicking :
driver.findElement(By.xpath("//select[#foo='bar']").click();
This is what works with the other menus, except I navigate directly to the "option" tag and click on it (don't need to click the drop down first)
b)Javascript executor
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
When I use this, no exceptions are thrown; however, the menu remains empty, which leads me to believe it is not being clicked on.
c)Actions
Actions builder = new Actions(driver);
builder.moveToElement(menu).click(menu);
builder.perform();
For some reason when I use this, the whole pop-up window with the drop down menus on it closes. :/ (I have double checked that it is not the close button being clicked)
I'm not sure if this is relevant, but Selenium has no problem finding the elements, it is just when I try to click them that it complains.
To summarise, my questions are:
1) What could make the menus different such that one is clickable and one is not?
2) How can I click on the second menu and choose an option?
Edit: I tried the solution found in the similar problem; unfortunately it does not work. The solution was to add an explicit wait since the element may not have completely loaded, this only leads to a timeout.
Using JavascriptExecutor is a workaround to interact with non interactable elements. I think it should never be used in selenium tests because it makes the test do things a real user wouldn't be able to do in a real life scenario.
The most plausible cause is that you are interacting with the wrong element, try to debug to identify the element returned by the used selector.
You can use chrome dev tools in debug mode :
1- Put a breakpoint at the exception line,
2- Use $x("//select[#foo='bar']") in the chrome console to get the element.
To select a value, you can use the org.openqa.selenium.support.ui.Select object :
new Select(element).selectByValue(value);

How can i locate web element for 'dropdown-toggle' window in selenium webdriver

I can't able to click 'Board' link for the 'dropdown-toggle' window using selenium webdriver.
I have tried
cssSelector="a.accordion-toggle" (or) linkText="Board".
But it is not working.
SO, kindly give me a solution for how can I locating 'Board' webelement.
Please refer the screenshot.
Thanks,
Vairamuthu.
I think the problem is visibility of that element. Maybe you're trying to click it when the dropdown 'Academic Year' is collapsed and link 'Board' is invisible in the browser.
I see two possible solutions:
Click 'Academic Year' (or scroll mouse on it if it works this way) before clicking 'Board'
Use click with JavaScript:
driver.executeScript("arguments[0].click();", element);
Where "driver" - RemoteWebDriver object, "element" - RemoteWebElement
Note that the second solution won't work if the element for 'Board' link is absent in the page source when dropdown is collapsed.

Java selenium. disable auto scrolling of the page

I'm using selenium of Java for automation tests and the browser is Firefox.
This is my sample code:
WebElement elem = driver.findElement(By.xpath(".//*[#id='main']/div/div[3]/div[1]/div/div[3]/div/div/div/a"));
Actions action = new Actions(driver);
Actions action2 = action.moveToElement(elem);
action2.perform();
The problem is that "moveToElement" action is triggering auto scrolling event of the page.
I want the page to remain as it was before without the scrolling.
somebody may know how can I disable this auto scrolling?
Thanks.
You can't. WebDriver scrolls elements into view when acting on them.
You can't disable the autoscroll.
Some possible workarounds:
You could try to fire a syntetic mouseover event over your WebElement if that would help your cause.
After the moveToElement(), you could try rescroll to your needed position with window.scrollTo() or for example the Page Up key. This would, obviously, break the mouseover on the element, but maybe it's what you need.
You can position your real mouse cursor over the element via the Robot class. This might get a little tricky, you might need to enter the fullscreen mode with your browser (or use this) and then handle the scroll offsets manually, if there are some.
All depends on your intentions, on what you really need to do with the element and why.

Categories

Resources