Searching an element in Chrome Browser after page load in Selenium - java

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

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.

htmlunit refering to element

I would like to programmatically issue a click on a web page and download the CSV file from the website. I'm trying to implement this logic with HtmlUnit library. The code that I'm trying to use:
HtmlPage historicalDataHtmlPage = webClient.getPage("https://finance.yahoo.com/quote/GOOG/history?p=GOOG");
HtmlPage downloadHtmlPage = historicalDataHtmlPage.getAnchorByText("Download Data").click();
HtmlUnit seems to have a problem finding this particular element (not sure why - i tested this solution on other websites and it seems to find anchors by text).
Can you please advise how can I fix this error or how can I refer to the Download Data element in any other way?
Thank you.
Please go through the links mentioned by #pvg and modify your question as per the guidelines.
Having said that, have you tried using getAnchors() which returns a list List<HtmlAnchor> and see what is the text you are getting inside the tag you want. Maybe there are other elements which do not match your text assumption of "Download Data".
Would have posted this as a comment but lack the reputation to do so.
Looks like the page starts with minimal content and adds you anchor later by doing some background requests.
Try to wait some seconds before you start searching for the anchor.
Also it is a good idea to use Page.asXML() to get an idea of the current status of the page (e.g. before and after the wait to see if something has changed.

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.

Selecting from the option list in "select2" in IE8 with webdriver

I have one problem simulating selecting from the option list in "select2" in IE8 using Selenium WebDriver. The problem is selecting from the dropdown elements which are not visible (needs to be scroll).
Another approach I tried is to send string to the input and to find it afterward - without no success, the string is sent to the input but the option list do not update by the entered string.
I'm not 100% sure if this works in IE8, but try skipping the step of clicking on the select element first, just go straight to clicking on the option element you want to select.
No guarantees, but this approach works for newer browsers where there is some oddity surrounding selecting options from dropdowns, in that you don't have to (in fact it works better if you don't) replicate exact user behaviour.
There are two ways to interact with invisible element:
First: Bring element into view using Selenium api and then interact with it. Following is for Ruby. There must be method in Java to achieve this.
element.location_once_scrolled_into_view
Second: Execute javascript on the element:
driver.execute_script('arguments[0].click();', element)
Above is for Ruby. But logic will help you to achieve what you want.

selenium webdriver Java: improve "if else" performance?

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.

Categories