Selenium Out of focus element - selenium

I have a scenario in selenium where I have a web element which is available on the page but to reach out to that web element sometimes we have to scroll down depending upon some business logic.
I don't want to use javascript executor and css selector. Is there any other way we can check if element is not clickable I can try to see if there is a scroll down scroller available? And my driver should scroll down and try to check that element once before it actually throw exception.

Let me try to answer your Questions one by one.
we have to scroll down : When you think of scroll its worth mentioning that scroll method is not a method of Webdriver, this is a method of JavaScript. JavascriptExecutor is an Interface which is available in package org.openqa.selenium.JavascriptExecutor. Inside this Interface we have a method called executeScript() – so whatever script you will pass as a string it will be executed by JavascriptExecutor.
I don't want to use javascript executor : We are not using anything separate stuff by the name JavascriptExecutor. We are using the same instance of WebDriver i.e. driver, but we need to downcast it so that it can act as a JavascriptExecutor to perform whatever script you will pass as a string.
css selector : Using css selector is not mandatory. Selenium provides you the flexibility to use any of the properties of a particular element available like id/name/linkText/xpath.
Let me know if this answers your Question.

Related

Dont get all the Web elements in the Arraylist of webelement selenium webdriver [duplicate]

Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI?
I would like to verify a scenario where element is clickable, I know web drive has method "ElementToBeClickable" however, I would like to know the inner working. Please help me on this.
Also,how to handle a scenario where the element is loaded in the DOM but UI shows loading in progress, how to wait for complete load?
Please let me know, if the UI is not loaded then will selenium directly call the DOM element or if UI element is being loaded then it will fail the execution? I would really appreciate more technical explanation on this.
Selenium can identify the presence or visibility of the elements as soon as they are present or visible in the HTML DOM. From user perspective you can invoke isDisplayed() method on an WebElement to examine if the intended WebElement is displayed or not. As per current implementation Selenium may not be distinguishing between loaded and rendered elements. The ElementToBeClickable method in ExpectedConditions class sets an expectation for checking if an element is visible and enabled so that you can click it.
When the element is loaded in the DOM but UI shows loading in progress you still have to wait for the JavaScript and AJAX Calls to complete loading the page so all the WebElements on the page becomes interactable. At most to wait for complete load you can set the pageLoadStrategy to normal but may still have to induce WebDriverWait for the intended WebElement to become present, visible, interactable or clickable.
Here you can find a detailed discussion on Page load strategy
Of-coarse if the UI is not loaded Selenium may not be able to interact with a few of the DOM elements.
Update
As per your counter question here are the different stages of an WebElement and the respective ExpectedConditions to check the stages :
Presence of an element :
presenceOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
Visibility of an element :
visibilityOf(WebElement element)
An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
Element to be Clickable :
elementToBeClickable(By locator)
An expectation for checking an element is visible and enabled such that you can click it.
Note : As per the docs Element is Clickable - it is Displayed and Enabled.

Robot Framework - Locating input element with accept attribute fails

I am writing an automation script for an avatar upload module with the following CSS locator:
input[accept="image/png,image/jpeg,image/gif,image/bmp"]
I am using Robot Framework's Wait Until Element Is Visible keyword to look for the locator above but is unsuccessful with the error:
Element 'css=input[accept="image/png,image/jpeg,image/gif,image/bmp"]' not visible after 30 seconds.
Increasing the timeout also doesn't work. Using the same in Chrome Dev Tools would successfully find the element. My guess is that the commas/slashes are messing with Robot's locator parsing. My question is: What is the correct way to write the locator?
Though present in the DOM, an element may not be visible/rendered. This is very often the case with file upload input elements - the UI renders something different, a button, div that had applied styling and fits in better with the overall design.
Thus a check is it visible will rightfully fail. Change your pre-usage approach to validate the input is in the HTML - this is actually the same as what you did in the browser's dev tools - with the Page Should Contain Element keyword, and proceed on success.
There is no problem with the CSS locator your are using. Maybe the element is in another iframe?

Serenity BDD - How to reload page elements using pagefactory

i'm having a issue regarding element loading using pagefactory:
#Findy(id = painelDeContole)
private WebElementFacade painelDeControleBtn;
The trick is,
on this menu i have to do a mouse-over action on "... mais" to open a sub-menu like this:
But when i call painelDeControleBtn.isVisible() it's return false. (Last image, second icon)
I need some way to reload the page element and truly verify if the element is visible after the mouse-over action.
I've already searched for some method to do this inside PageObject and WebElementFacade but hasn't found any.
I'd like to maintaing the usage of pagefactory if possible..
Because of the way #FindBy loads elements, it may be present on the screen but not yet visible, or it may not wait until the element appears before returning the result. To do this reliably you will get better results with dynamic lookups, e.g.
$("#painelDeContole").isVisible();
or
$("#painelDeContole").waitUntilVisible();

How to ignore `other element will receive the click` and still click on the element

I am using Java and Selenium to write a test. I am going to click on a web element that is covered by another one so I receive the error other element will receive the click. I do NOT want to use Select so how can I click on the web element that is covered?
These are things that I have tried:
action.click(dropdown).build().perform();
action.moveToElement(dropdown).sendKeys(Keys.ENTER).build().perform();
dropdown.click();
Also there is no need to use scroll as the element is on the page but just covered by something else.
You can use JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
I would not do this as it could miss potential defects. Not sure why you want to perform a click on hidden element.

Selenium, what is the best practice to deal with element is not clickable

I am using selenium 2.46 (firefox driver) to develop an application. There are a lot of element.click() in my code. Sometimes that elements are not visible or not clickable make the application throws selenium exception.
To resolve that issue, i use WebdriverWait(driver, 10).until(...) for each single element which needs to be clicked.
My question is there is any other better way Or design pattern that can help me to solve the problem best.
Or at least i dont have to use WebdriverWait for each single element needs to be click().
You cannot avoid WebDriverWait. If you send a webdriver click command, webdriver will blindly assume that "element is clickable". You need to instruct webdriver to wait because your element is special and needs some synchronization before it can click on it. I don't think you need to do this for every other element. You can incorporate ExpectedConditions so that you can keep your code snippets manageable and small. So something like,
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("foo"))).click();
The other option you can try other than clicking is hit enter on respective element, for that you can refer ID of that element.
driver.findElement(By.id("elementid")).sendKeys(Keys.ENTER);
use implicit wait instead of explicit wait and give the expected condition till the element doesn't visible on screen.
for more info you can check
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#invisibilityOfElementLocated-org.openqa.selenium.By-
Hope this will help you