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.
Related
I am working with JavaSE and Selenium WebDriver within Chrome. My task is to find a set of input fields and do stuff with them. The issue is that I have to do stuff in the presented order they are available on the web page.
So I would find them via XPATH, because that's what works in the given web page. Let's say that I have a set of inputs on the following path: .../form/div/div/div
However for reasons I cannot say, certain type of input fields (such as text and numbers) are in the following path: .../form/div/div
The problem is that one set of the inputs are one div 'deeper' than the others, so when I save them to a List<WebElement> with driver.findElements, i can't really save their order.
I thought of finding the inputs with id, but the id names have a space in it which Selenium apparently dislikes. I am not sure if relative XPATH could be of help in this case.
Your comments are appreciated.
I made the mistake of not reading enough about XPATH. What I was looking for was the 'and' operand within an xpath expression. If you are a beginner like me, please read about it on w3schools.
Basically the following code solved my issue, as a workaround:
driver.findElements(By.xpath("//input[#required=''] | //select[#required='']"));
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();
}
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.
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.
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.