Selenium Click method performs click slightly outside the element boundary - java

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"));

Related

Is there a possibility to refresh a certain frame in Selenium

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();");

How to scroll till an element that is inside an i frame

I need to click some element that is inside an iframe. When page is loaded these elements are not visible and I scroll down to make them visible. I tried as follows:
Move to iframe and switchTo(frame)
javascript.executeScript(argument[0].scrollIntoView(true), webelement)
Scrolling is happening only once. When I click first element. I need to scroll down and click the next element (it is a submission form in ui). All elements are in iframe.
Can anyone suggest any idea I'am using java with selenium.
Try this:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", myIWebElement);

Selenium iFrame input text

I am using Selenium to build a test automation where the html is in an iFrame, I was able to find online the lines of code to activate the iFrame, click on a link, and press a button and they are working fine -see following lines:
driver.SwitchTo().Frame("06634000000BVL6");
driver.FindElement(By.LinkText("Loan Details R1")).Click();
driver.FindElement(By.XPath("//button[contains(.,'Edit')]")).Click();
I needed to input a text within a textBox in that iFrame, but I couldn't handle the ID or the Class, below is the HTML for the input:
Any thoughts ?
Thanks for your help
I can't comment yet, so this will be part answer, part comment.
Based on what you have posted, it looks like you may not be in the correct iframe and we don't have enough of the code from the webpage to tell if there is an additional iframe.
I'd also like to see the code you are using to write text to the field, you may have an issue there.
If you don't and the only issue you have is the selector then try the following. Go get the developer version of firefox. Navigate to your webpage in firefox. Inspect the element where you want to write text to. Once you are in the inspection screen, there is a path bar that can scroll left and right at the bottom of the screen. Check that to confirm that there is only the one iframe you mentioned and that you are in the correct iframe. If that is the issue, you should be good to go, you're code above works for switching between frames. If you are in the right iframe, then try a different method of finding the element for the text box. I have had the most success with hard to find elements by using the cssselector. To get the cssselector for the element right click on it, navigate to copy and then cssselector. From there your code should look like this (using c#):
driver.FindElement(By.CssSelector("INSERT CSS SELECTOR HERE")).SendKeys("Text");

Selenium WebDriver Java cannot find element in Chrome to scroll to

I have a Web application with a scroll-able menu on the left hand side. My code is pretty simple:
WebElement elementToScrollTo = driver.findElement(By.xpath(locator));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", elementToScrollTo);
locator is the link to the web element/menu item I want to click. The problem is in Chrome, the first line throws a NoSuchElementException. So obviously I cannot scroll to something that cannot be found in the first place.
However, what's weird is that in IE everything works perfectly. The element can be found and the menu scrolls. After which I use the element (i.e. click on it).
I cannot scroll the entire browser window as I only need the menu panel on the left hand side to scroll.
I have the latest Chrome (60.0.3112.78 (Official Build) (64-bit)) and chromedriver 2.30.
Note: I've actually figured out the problem. The reason it cannot find it is because it doesn't scroll to the parent of the menu item, the level 2 element. so the second line (scrolling) doesn't do anything in Chrome and because of that the driver will not find the menu item on level 3 (the child of level 2). So maybe scrolling doesn't work in Chrome?
It might be problem with the HTML elements located within a frame. To deal with such elements you need switch the frames first.
Can you try this command:
driver.switchTo().frame()
Also, try to use implicitWait if the above one doesn't work. Maybe, your element is taking time to load and therefore web driver fails to locate it.
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

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.

Categories

Resources