selenium webdriver Java: improve "if else" performance? - java

I am trying to achieve the following:
If a specific element exists, click it and go back to the home page.
Otherwise, just go back to home page so the test continues on without failing
I have come up with the code below, this but it is really slow. I am not sure there is any better way to implement this? Any comments will be appreciated!!
boolean exists = driver.findElements( By.id("xxx")).size() != 0;
if (exists)
{
driver.findElement(By.id("xxx")).click();
driver.findElement(By.cssSelector("xxx")).click();
}
else
{
driver.findElement(By.cssSelector("xxx")).click();
}

I worked out what's slowing down the performance. It is this line:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Because of that statement, it will wait for the element to be verified for 30 seconds.
After changing it to:
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
.. it now works like a charm...:)

What are you using for CSS selectors? You might be able to improve the performance just by tweaking those. Another thing that will slow it down is when the page has too many DOM elements.
It would be helpful to see the CSS selectors and an example of what DOM elements you are scanning over.
For example, if your page is full of 1000 DIV elements, with a class like this:
<div class="smallItem">...</div>
<div class="largeItem">...</div>
<div class="smallItem">...</div>
and you use a css selector like this:
".smallItem"
to select all of the DIV elements, it has to scan over each DOM element and compute on the class attribute.

Related

Can I obtain this input element with Selenium?

I'm new to Selenium.
I have been looking all the ways possible to resolve this problem (at this point I think it is just unsolvable) I have a web page (can't share) with this input:
/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div/iframe/#document/html/body/div[2]/div/iframe/#document/html/frameset/frame/#document/html/frameset/frameset/frameset/frame/#document/html/body/div/form/table/tbody/tr[2]/td[2]/input
(As you can see the structure has a mix of frames, iframes and framesets; some of them have id and other names some none)
The problem is that Selenium will not find the element by any method I
have tested
. First I tried with a simple driver.findElement(By.all of them)
After they didn't work I start looking on the web and I couldn't find why I couldn't get a handle of this.
It is the only input in the web page and has an id as attribute, so it should be easy.
This is the class where I have been trying to make it work, If you want to check my unsuccessful work I focused my last efforts on the attempt number 8.
As you can see I have been trying to obtain it by several ways, the frames really seemed an issue but it was not them, nor the waiting.
Is it really no way of getting this element? Is it one of those cases
where Selenium can't automate? Or it is that I'm missing something.
object IS visible and there is not even a scroll bar, the whole page fits in the screen perfectly, Xpath was one of the first choices I tested, didn't work
Thank you in advance.
I don't know if this is the problem, but is the element you trying to use visible or you need to scroll in order to view it? If it's not visible try using this to scroll a little bit :
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
Another possible solution is to try use the absolute xpath instead of relative. There are several tools to find the absolute xpath of an element in an html page. Then you can use
driver.findElement(By.xpath(absoluteXpath));
After a lot of try I realized that selenium was switching to a different frame at the last switchTo. This is probably a bug, but I modified the way I was getting the last switch to to instead of
var wait = (new WebDriverWait(driver, secsToWait));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameNameOrId));
To
driver.switchTo().frame(driver.findElement(By.xpath("//frame[#"+attribute+"='"+frameNameOrId+"']")));
So it finally worked and obtained the input normally.

Searching an element in Chrome Browser after page load in Selenium

I am trying to search for an element in Chrome after page load on a particular page. However, when I inspect the page, I notice that the HTML tags are not loading dynamically once the page loads. Hence, the driver is searching for the element after page load and the elements available are still the ones which were present before page load and the search is failing. I have tried all the locator techniques. Also used Thread.sleep and wait commands to wait for the page to load, but to no avail. Let me know if there is an alternative for such a problem.
Even though your question might not be complete, the best way (to my knowledge) to wait for a page load in java is:
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
For the most part, i try to check whether the element is displayed and enabled by using either id, xpath, or name to find it.
You can add this to your code, and apply a break point. it might be there but might also be hidden. without a stacktrace of the error is difficult to know what is causing you issues. hope this helps.
WebElement element = driver.findElement(By.xpath);
if (element.isDisplayed() && element.isEnabled()) {
element.click();
}

Getting the count of elements in a pop-up using Selenium and Java

I am writing test cases using Selenium and PhantomJsDriver in Java
Selenium - 3.0.1
PhantomJs - 2.1.1
Expected Scenario :
Open a pop-up page and find the No of elements inside the pop up page (Actually the items getting displayed inside the pop up).
At any given point of time there can be only 3 elements inside the pop up. So i am doing an assert here.
Below is the Code for the same
With Class Name using findElements method
List<WebElement> foundItems = By.className("className").findElements(driver);
int count = foundItems.size();
With Xpath
int count = driver.findElements(By.xpath("//div[#class='className']")).size();
In both the cases i am getting the count wrong, i always get the count as multiple of elements which are inside the pop up page.
But if i iterate over the list and use .isDisplayed() method and maintain a flag it is giving me the correct count.
I think it might be an issue of cache or localStorage issue which phantomJsDriver maintains.
How could i clear the Cache or LocalStorage using Selenium and Java.
Or is there any other way to get it done.
You should provide HTML of your page so we could help you.
As for the local storage, you can delete it using javascript, this should work fine:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.localStorage.clear();");
The answer depends on two things:
What exactly you are trying to get the count of
What method the page uses to hide controls
In my example below, I show an XPath that will show the number of input controls excluding those designated as type 'hidden'
driver.findElements(By.xpath("//input[not(#type='hidden')]")).size();
On a page I tested, //input returned 6 items, but I only saw four. Adding the extra qualifier brought the number found to four.
Alternatively, you can get the count of each type you are looking for, and accumulate the numbers, i.e.:
int eleCount = driver.findElements(by.xpath("//input")).size() +
driver.findElements(by.xpath("//a")).size() +
driver.findElements(by.xpath("//h1")).size();
Would return the numbers of input elements, anchors, and headers correspondingly. If you need to know more, then check for more tag names.

Element not exists, takes a lot of time to Execute next line of code

I'm working as Automation Engineer using selenium, I have found one problem with selenium,
If Element is not exists in the HTML page or DOM its take a lot of time to find that element more than 5 min after 5 mins its executes next line of code I want if that element not exists in the page it immediately go to the next line of code but it takes more time.In Some cases element exists in page so I did if element exist then come this code otherwise go to else code I have a lot of cases like these so it takes a lot of take to execute complete code,
I tried with all possible ways like list ,try and Catch but unable to reduce time, Can you give any solution for this in selenium?
When you create your driver define implicit wait
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
This will try to locate the elements for up to 5 seconds, you can change it to whatever suits you.

Is there a way to call nextElementSibling in Selenium Webdriver with Java?

I am working with a really messy page structure and the fragment that I am stuck on looks something like this:
<div>
<h3>...</h3>
<ul>...</ul>
<h3>...</h3>
<ul>...</ul>
...
</div>
I want to get one of the <ul> elements, so I could dig deeper into it and retrieve the actual value that I really need from there (it has a table inside it). Currently I am able to get the <h3> element that precedes the <ul> I am looking for. Since ul-elements don't have any unique identifiers that I could use to get them directly, I am hoping to achieve it by getting the element that comes after the h3-tag (on the same level). Is there a way to get what seems to be nextElementSibling?
Thank you!
NB! h3 and ul elements don't have strict sequence number - there may or may not be a few elements before them, so getting an n-th child does not seem to be an option there.
You can achieve this with either xpath or by executing some javascript.
Xpath:
driver.find(By.xpath("//div/h3/following-sibling::ul[1]"));
JavaScript:
JavaScriptExecutor jsExec = (JavaScriptExecutor)driver;
WebElement ulElement = jsExec.executeScript("return arguments[0].nextSibling;", driver.find(By.cssSelector("div h3")));
Hope that helps!

Categories