I have a JavaScript code embedded in a Java method to retrieve properties of any element clicked upon in a web page.
That is, I navigate to a specific URL by creating an instance of Selenium WebDriver from my Java application and click on an object in the page that gets loaded. After the single click on the object, a HTML page opens listing the name, type, id, class name etc. associated to that object as a radio button group. I select one option from the list and click on OK. What I wanted to know is, can I push the selected data from the HTML back onto my Java application on the click event on the HTML?
Any help appreciated! Thanks! :)
WebElement interface of selenium Webdriver gives a method
getAttribute which provides value of a given attribute of the desired html element.
I hope this method is what you are looking for.
USAGE:
String field_value;
field_value = driver.findElement(By.xpath("xpath Locator")).getAttribute("attribute name")
The information which you want to get should be in any of the attribute associated to that element.
Similarly if you want all attributes associated with the element in one go you can use something like this :
((JavascriptExecutor)driver).executeScript("return arguments[0].attributes);", element);
The above will give you a comma separated list of all attributes on which you can apply your coding logic to separate them and get the corresponding values using loops.
Related
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.
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.
Automation Specification- Java based WebDriver
Hi
i'm working on a webdriver automation(Java code) to automate bus booking. Here is my scenario....
(1)give Departure & Destination - DONE
(2) Pick the Date - DONE
(3)Search Bus -DONE
(4)View Seats & Select- I Struck here
Now as a search result, i got a list of buses with the option to view the seats.
the list of buses are shown in a dynamic-DataTable which contains an image called 'view seat' in each row. A click on the viewseat-image will give me the seat-layout where i can select the seats. found the viewseat-images are having different id's & the same class name. Now i need to locate and click on the viewseat-image whichever i want, using its id.
But the situation is
* the data table is dynamic, So the id's keep changing which doesn't allow me to locate it by a static id.
* id is the only unique thing in that coding to differentiate the viewseat-image from another. so i can locate it only if i get the id correct.
Now my idea is to, get the runtime id's of all viewseat-images, using the unique class name and store in variables and use the id's to locate the element.
So let me know if there any possibility to get the id's of all images in that data table, using the class name.
You can use absolute path and take the location of the seat which will reduce your efforts, to take the absolute path you can install a plug-in in firefox called "firepath", after installtion you will have an option for selecting either AbsolutePath or RelativePath. With the absolute path you need not use ID to identify the element which is created dynamically. The path is identified using the location of the elements within the tags.
Example for Absolute Path:
//div/div/div/table/tbody/tr/td[2]/div/input[3]
List<WebElement> elements = driver.findElements(By.className("classname"));
for(WebElement ele : elements) {
System.out.println(ele.getAttribute("id"));
}
driver.findElement(By.xpath("//a[text()='View seats']")).get(index).click()
You can try this method.But i'm not sure it can work out.I'm looking for a general way which can solve the problem at dynamic pages.
Here when the dynamic-Data Table is coming so you can store the size of in the table, Like :
int size = driver.findElements(By.xpath("//*[#id='demo_ID']/tbody/tr")).size();
And can set a loop which would read every available row of the table or you can click on them one by one. Like :
String strGetContent = text.getText();
or
text.click(); // As per your need
Now you can read or click every row of the table, although rows are coming dynamically.I hope it helped you!
For more explanation or an example I would prefer: How to Automate a Dynamic page through Selenium Web Driver
How to verify if Tab on Web Page is selected using Selenium RC
I wanted to one very simple thing. Does anyone know using selenium RC Python Client how I can know if a Tab is selected on web page?
By tab I mean the following examples from the following link-
http://esdi.excelsystems.com/wsexmp/DIVTAB.pgm?wsnum=00096
I have used focus(), isSomethingSelected(), isVisible() but didn't get the solution.
I need to verify that the specific tab is selected by default after webpage opens. Isn't there a method like is_tab_selected(tab_locator)??
please provide the clear solution pls..
I have used focus(), isSomethingSelected(), isVisible() but didn't get
the solution.
These methods use the common HTML element terminology.
focus() is for elements that are focused, meant as when you click a focusable element, it has a focus on it. To see what I mean, you can loop through the focusable elements on your page via pressing the Tab key repeatedly. This changes focus.
is_something_selected() is for selecatble <option> elements (which are the children of a <select> element)
is_visible() tells you whether an element is actually visible on the page or whether it is hidden via CSS.
Anyway, there's no is_tab_selected(tab_locator) method, because there's no such thing as a tab. In your case, your "tabs" are just simple clickable <a> elements which have a class attribute tab-active or tab-disabled based on their state.
Therefore, if you wanted, for example, to know whether the second tab is active, you would do
is_element_present("css=#tab2.tab-active")
This will return either true or false based on whether the tab is selected or not.
Or the other way around, if you wanted to know which tab is currently active, you would do:
get_attribute("css=.tab-active#id")
This will return the id of the selected tab.
You have to find a unique tag in your webpage. You can do
driver.findElement(By.xpath(".//tagname"))
If the above line doesn't throw any exception, you can confirm that you are located in your webpage.
So, I was trying out a test case on a website and Selenium registers the events perfectly. Now, if I have to search for a particular class and get the innerHTML of that class, how would I do it assuming that I am using Java as the driving language?
Just to be more clear. I have a class like this:
<h1 class="classname">.....</h1>
I want to get the entire text between those tags.
And finally, if the ids of the buttons on the page are dynamically generated, how would I test the clicking action on them? These button, I presume are using ajax. When I click on the button, Selenium generates this:
//div[#id='c4aef94de622f51859827294']/div/div/div[1]/span[2]/span/span[3]/a
and the actual HTML of the button is this:
<a href="#" bindpoint="forward" class="ButtonForward"/>
Is it even possible to click on the button?
You could use the getHtmlSource command to get the entire HTML source, and then use something else to parse the HTML in Java and extract the contents of the target element.
You can locate elements without using an ID, for example if this is the only such button on the page you could locate it:
Using CSS locators:
selenium.click("css=a.ButtonForward");
Using XPath locators:
selenium.click("xpath=/descendant::a[#class='ButtonForward']");