How to select one from duplicate tag in page in java in selenium webdriver - selenium

I am using Selenium WebDriver and I have number of items on a page and each item on page is a separate form type.
I have saved all of these form elements in a list and I am iterating over every item in an attempt to get the name of the element by using the "alt" attribute.
However when I try to get the "name" attribute from the input element it is always returning the first input tag found on that page, not the name attribute of the element I have currently selected.
The syntax I am using is:
((Webdriver imgtags.get(i)).findelement(By.xpath("//input[#name='qty']")).sendKeys ("100");
I have also tried to get the id from the tag by using:
((Webdriver imgtags.get(i)).getAttribute("id");
It's returning a blank value, but it should return the value of the id attribute in that input tag.
I also tried to get the id by using .bytagname but as id is an attribute it is not accessible

Try:
(driver) findElement(By.xpath("//*[contains(local-name(), 'input') and contains(#name, 'qty')]")).sendKeys("100");
To answer the comment by #rrd: to be honest, I have no idea why OP uses ((Webdriver imgtags.get(i)). I don't know what that is. Normally, I just use driver.findElement[...]
Hoping that he knows what works in his framework :D

Selenium Xpath handling is not fully compliant and it does not always treat // as a synonym of descendant-or-self.
Instead try tweaking your code to use the following Xpath:
((Webdriver imgtags.get(i)).findElement(By.xpath("./descendant-or-self::input[#name='qty']")).sendKeys("100");
This will base your search off the currently selected WebElement and then look for any descendants that have a name attribute with a value of "qty".
I would also suggest storing your imgtags array as an array of WebElement e.g.
List<WebElement> imgtags = new ArrayList<>();
This is a much better idea than casting to WebDriver to be able to use .findElement(). This will cause you problems at some point in the future.

Related

Not able to locate webelement

I am not able locate a webelement, this web application opens in Internet explorer only and I have used all the possible ways to click but no luck.
Xpath locators that I have tried :
"//form[#id='Form1']//a[contains(text(),'Age Range')]"
and
"//form[#id='Form1']//a[#id='rptTables1_ctl07_hlTablename1']"
also I have tried click on the element using action class and javascript as well.
Attached DOM in the URL, please have a look here
In the node a, id value is not static so you can't locate that element using the id value but you can use partial id value for example, looks like rptTables1_ is unique in id value and the remaining part is changing so applying contains() on this may works.
Try the below xpath if there is only match :
//a[contains(#id, 'rptTables1_')]
Try the below xpath by providing the matching index if there are multiple xpath matches :
(//a[contains(#id, 'rptTables1_')])[Matching index number]
for example if the matching index is 3 then you can write like this (//a[contains(#id, 'rptTables1_')])[3].
Or you can use the Advanced Performance Parameters Panel Topics text to identify that element.
//a[contains(text(), 'Advanced Performance Parameters Panel Topics')]
Again if there are multiple matches then try to use indexing method as mentioned above.
Or you can try the below modified your xpaths :
//form[#id='Form1']//a[contains(#id, 'rptTables1_')]
or
(//form[#id='Form1']//a[contains(#id, 'rptTables1_')])[Matching index number]
or
//form[#id='Form1']//a[contains(text(), 'Advanced Performance Parameters Panel Topics')]
I hope it helps...
I haven't tried any automation in IE at all but in firefox, sometimes I encounter those issues and my work around other than the .click() function is send_keys(Keys.RETURN). Also, i'm using time.sleep(x) before clicking or sending keys to make sure the element has been loaded.

not able to find element using id in findelement in selenium

I am not able to find the element using "id" in selenium as the id is randomly changing in every session of execution so the same id i am not getting in next execution.
As there is no other unique property is there to identify the element.
code sample
You didn't specify a language so I'm going to give you Java. You can do this by using the CSS class or probably a better choice (because of likely uniqueness) is data-lynx-name.
By CSS class
driver.findElement(By.cssSelector("div.http-lynx-json-org-text-input"));
By attribute
driver.findElement(By.cssSelector("div[data-lynx-name='username']"));
You really should read the question that I duped this one to:
Find element by attribute
Also read more about CSS selectors,
http://www.w3.org/TR/selectors/#selectors
You can use XPath.
String xpath = //div[#data-lynx-name='usernameLabel'][text='User ID']/following-sibling::div[1]
The above XPath will will find the div tag containing text 'User ID' and finds the next div which is is the required textbox.
It seems that you can even use the attribute 'data-lynx-name' attribute of the textbox div tag directly.
String xpath = //div[#data-lynx-name='username']
Selenium
driver.findElement(By.xpath(xpath));

how to identify an object with the same name with name property

I want to find out an object with same name with different value? Here i am interested to identify based on the name only. Do we have index like property provided in QTP. In QTP, if two buttons with same can be distinguished by index, first button with index 0 and second button with 1.
Is there way to do the same in WebDriver?
I want to identify object with name meaning "By.name". How can i do that?
Thanks,
Uday
There are multiple options to achieve it (examples in java):
using findElements and get the appropriate element from the list of resulting web elements:
List<WebElement> elements = driver.findElements(By.name("test"));
WebElement element = elements.get(0);
use xpath based approach (indexing starts from 1 here):
WebElement element = driver.findElement(By.xpath('//input[#name="test"][1]'));
You can also use jQuery style syntax in your search..
For example in Chrome tools $('css selector')[0] will get you the first occurrence of some element.
Selenium comes with a JavaScript driver so you could instantiate that and use that against your site to leverage jQuery.

webElement.getAttribute() is not working in selenium

I am using selenium for automating test cases for web application in that I have to get tool tip text
I tried
String result =element.getAttribute("span");
but this is retuning null. how can I get the text ?
element.getAttribute("span");
span must not be an attribute of the given element. I guess you've misunderstood, the getAttribute() method.
For e.g.
Example
For the above anchor tag, in order to get the title attributes' value, you can use :
element.getAttribute("title");
where element refers to the above anchor element.

Selenium nth match by id without common parent

I have to test some complicated web service using Selenium.
Problem is that ids of elements are changing from session to session.
For example there is bunch of inputs each have id with prefix textf_id_DComboBox_ and ends with a consecutive numbers, starting number is random (session dependent).
Those inputs doesn't have a common parent so nth-child doesn't work.\
I can find first input by using selector: css=input[id^='textf_id_DComboBox_'] but I have no idea how to find next items (1-7) which match this selector.
I've found some suggestions on stackoverflow that xpath selector should be used, but I was unable to adopt examples for my use case.
Update:
I have also alternative selector which captures first input: css=td.DForm_treeGridNoWrap input.
You can use this XPath in order to select all inputs that contain a common id:
string comboBoxXPath = "//input[contains(#id, 'textf_id_DComboBox')]";
List<WebElement> comboBoxElements = driver.findElements(By.XPath(comboBoxXPath));
At this point, you can iterate through the entire collection, or you can select which one you'd like to interact with by using an index:
comboBoxElements[1]
comboBoxElements[2]
comboBoxElements[3]
etc...
Well, that descrption does not help that much. You can try these tricks:
You can call findElement on WebElement This trick will probably not work, because those IDs do not have common parent. But if they are wrapped, say, in table, you can find the table first:
WebElement table = driver.findElement(By.id("the-table"));
And then to find all input in such table:
List<WebElement> inputs = table.findElements(By.tagName("input"));
Install Selenium IDE to your firefox and try record testcase by using it. You can play with target in Selenium IDE.
Dirty approach
List<WebElement> allInputs = driver.findElements(By.tagName("input"));
Will find all inputs in such page.
Footnote: The code is Java and driver variable is considered as healthy instance of WebDriver