In Selenium how do I find the "Current" object - java

I would like for Selenium to navigate a menu via arrow keys--Starting with clicking the top menu item then pressing "DOWN", "DOWN", ...
The problem is that you have to always supply a specific element to send the "DOWN" to.
Is there any way to get the current element?
I tried:
by.xpath(".")
but it said that the expression was unrecognized or didn't return a proper object.
I expect I'm jsut missing some stupid trick.

In Selenium 2.0, if you are using WebDriver to drive the tests in the browser, you can use the WebDriver.TargetLocator class to get the element in focus, in a window/frame:
WebDriver driver = ... // initialize the driver
WebElement currentElement = driver.switchTo().activeElement();
If no element is in focus, the active element would turn out to be the body of the document being displayed, which might be the case when you launch a new page, for instance. When you invoke methods like click, sendKeys etc. you'll find the WebElement returned by the above invocation will always represent the element in focus.
This was tested using FirefoxDriver, and I would suspect that the same would be true of other drivers, except for the HtmlUnitDriver and similar drivers that do not use a full-fledged browser under the hood.

in python:
element = driver.switch_to.active_element

Don't know of a more straightforward way than accessing document.activeElement
How do I test which element has the focus in Selenium RC?

In Ruby/Capybara:
page.driver.browser.switch_to.active_element
Note that this returns a Selenium::WebDriver::Element not a Capybara::Node::Element.

Related

Can we use one pageobject among different pages?

I am working on one selenium project. My scenario is, I have multiple forms one after another. After filling them I have to click on the Next button. I have 4 pages with the next button and they have the same XPath. When I try to use the same web element for the next button it works once but when I try to use that same thing again on a different page It shows element intreactable error.
I would try the following to identify why you get the interactable error (it would help to give us the error at least as is found on the Selenium Documentation)
try to click using javascript executor,
try to add webelements for each action you need to perform (so 4 webelements for each 'Next' button). If it works in this way maybe the webelement is not refreshed after you use it (maybe is a static field). Try also to work with Page Factory pattern.
If nothing works, we should receive more information to be able to help.

Unable to click on a button which is auto-focused by a script

I'm trying to click a button which is auto-focused by a script in the html.
I've tried both absolute path, relative path but no luck. pretty much anything I can think of. Attached is a screenshot which will display that the xpath is locating the element successfully on the web but when I use it in selenium, it can't find the element.
I've also tried waiting explicitly for 10 seconds for the element using a generic function. (Check the screenshot) This function is working for all other buttons that were called previously except for this one.
Something CAUGHT my eyes is that There's a method being called which auto-focuses the button. I might have to turn the focus to the entire window or the page in this case. I've also tried sending the enter key but still no luck.
Question_1: Does anyone have any solution?
Question_2: Does anyone know how to switch focus to the page?
Question_3: Or anything.
Any suggestion is highly appreciated.
Use Java Script click instead of click. Like this, Hope this will help.
WebElement element = driver.findElement(By.id("SubmitButtonId"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
To switch focus to parent window/default content window you should use switchTo() method on driver instance.
driver.switchTo().parentFrame();
OR
driver.switchTo().defaultContent();
You can use following two statements to know which element is currently focused.
driver.switchTo().activeElement();
driver.getPageSource();
If you try to click on input tag , your script will fail. try to find correct locator only. it will help or try submit() function as you said, this element is auto selected.
I thinks you need to bring element in to view before clicking on the same:
the code for that is:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)", webElement);

Can not find selenium WebElement twice

Working with selenium and InternetExplorer 11, I can navigate around this site.
I got a method that finds an input box on the page then inputs text. It works the first time, but when I go to another page it says that element does not exist and the page just refreshes randomly.
I a m just using a simple wait:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'Enter Name')]")));
WebElement nameField = driver.findElement(By.xpath("//*[contains(text(), 'Enter Name')]"));
It crashes and says the element can not be found during the wait.
I am not sure if this is just site specific or if I am doing something wrong.
If anyone has any idea I'd appreciate it :)
Here is the Answer to your Question:
As you mentioned when I go to another page the entire HTML DOM gets changed and now if you try to locate or use findElement() with a previously found id/name/css/xpath Selenium will fail to do that.
So when we are on a new page we have to search for the element again using the current id/name/css/xpath in-order to use click() or sendKeys() methods.
Let me know if this Answers your Question.

Unable to retrieve Textbox Value In Selenium Testing

I am new to Selenium testing, I am trying to put the value into the textbox with the help of xpath, name & id. (java using eclipse IDE):
driver.findElement(By.xpath(".//* [#id='txtEmpDepAddressLine1']")).sendKeys("2nd Main")
driver.findElement(By.xpath(".//*[#id='txtEmpDepAddressLine2']")).sendKeys("Bangalore");
While running, textbox is not retrieve the from the above code. Can anyone help me out to solve this problem?
Use By.id instead By.xpath
If you insist xpath then:
Start with: // instead: .//
Make sure the id value is unique on page.
if using xpath in some browsers id or ID makes differnce.
It is difficult to say without the HTML, but I'd suggest a few things:
Open your page, go to the Google Chrome DevTools console, and verify you can locate the element using your xpath. So you'd type:
$x("//* [#id='txtEmpDepAddressLine1']")
and verify the text box is located.
If the sendKeys isn't working on the element at that id, I'd wonder if it is not on the text field itself, but on a containing table or something like that. Make sure the element located by the id is the input, so the sendKeys will work.
By
In the text box it is fetching the value but it can't able not store
in the text box field
do you mean you can see selenium typing those values into the textbox and then the values disappear?
If that is what is happening, it is possible there is some javascript running on the page while selenium is entering the values in the textbox. you can try to put a wait before selenium enters the value. If waiting resolves the issue, you will need to write a method to detect when the javascript is done.

Selenium WebDriver set Attribute having webelement

Currently I have a problem - I want to switch out of current frame to parent. Application have a lot of of inner frames, so using driver.switchTo().defaultContent() is not the case. In this discussion possible solution is suggested - you should remember all frames you visited and and iterate over them from the top using
driver.switchTo().defaultContent()
The main problem is in that fact, that in my application I, sometimes, forced to use switching to frames located by xpath and other locators - use switch by WebElement. And as it is already mentioned in discussion above after switching all located WebElement becomes invalid and can't be used for switching. So I should have some way to find this element inside frame again. Although i can locate index of this frame element executing some js calls(i.e (WebElement)executeScript("window.frames[i]").equals(myWebElement)), but it is not the case as index may change when some frames deleted from page. Either I can try to find frame names and id's but they may not be specified.
So I see the only possible solution - set some attribute to frames that parent of currently selected - for Example attribute active with value true. Also I should be able to do this for all methods that can switch frame (there exist 3). In all methods I can receive WebElement representing frame, but as far as I know to change attribute value you should execute some js code. So the problem is how to locate current WebElement in js.
Thanks for reading this all.
look at this code snippet above.that's the best way you can locate the current element in Js
passing the element as an argumet to execute script and referring to it in js using arguments[0]
WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);
you would have to implement a custom logic using javascript
if the element has id then you can use the findElementById() method available in javascript else use the findElementsByName() and then iterate over the array and match other properties to narrow down to your element

Categories