How to declare, same webelement with different ids using Findby? - selenium-webdriver

For instance, is it possible to define:
#FindBy(By.id("id1") OR By.id("form1:id1"))
public WebElement button
So that button having either "id1" or "form1:id1" should work fine?

You can use the #FindBys annotation, the syntax is:
#FindBys({#FindBy(id = "foo"),
#FindBy(className = "bar")})
The JavaDoc describes it here:
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/FindBys.html

Well,
use whathever you want, as long as it works
Personally I would use #FindBy(By.id("id1")) but it is just point of choice.
Also, there is no value added in referring same element twice with two different methods. It will only cause mess in your code
EDIT
As I understood your comment, there is element on the page which constantly changes its ID. If you need to refer to such elements, try using xPath See for example this xpath tutorial
The idea is that you will point to some place in the DOM rather than to specific ID

Use Xpath or CSS selector to do that. Or Java to store the ID name in String and then you can fill it to your Id.

Related

How to iterate over elements in selenium

Actually i want to read emails one by one in junk folder of "outlook:live" and mark emails "Not spam".
emails = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,"//div[#class = 'xoCOIP8PzdTVy0T6q_uG6']")))
This xpath matches 400 instances. I want to make a loop to select one email at a time like select first email, click on the div and perform action and then 2nd email and so on. I'm trying this
emails = WebDriverWait(driver,
5).until(EC.element_to_be_clickable((By.XPATH,"//div[#class =
'xoCOIP8PzdTVy0T6q_uG6']")))
for count in range(0,len(emails)):
(emails)[count+1].click()
Please help me know where im doing wrong. Thanks in advance
It appears that the function you're using to return the clickable elements is only returning a single element, so you'll have to use a different function, make a change in your logic, etc.
For instance, you could use Selenium's find_elements_by_xpath("//div[#class = 'xoCOIP8PzdTVy0T6q_uG6']") which will return a list of WebElement object(s) if the element(s) are found, or an empty list if the element(s) is not found. This will, of course, not take into consideration the possibility of the elements not being completely loaded on the page. In my experience, just slapping a time.sleep(10) after you open the page is "'good enough".
I recommend making sure your elements can be discovered and interacted with first to make sure this isn't all in vain, if you haven't already.
Another option is to add another function, something like a elements_to_be_clickable() function, to the Expected Conditions source code.
From the Expected Condition documentation, I've done some research and it looks like the element_to_be_clickable() function only returns a single element. Moreover, from the source code, said function mainly makes use of the visibility_of_element_located() function. I believe you could follow similar logic to the element_to_be_clickable() function, but instead use the visibility_of_all_elements_located() function, in order to return multiple WebElements (since visibiilty_of_all_elements_located() returns a list of WebElements).

Angular JS - How to find the element in DOM having the xpath?

How can I use xpath instead of id to find the element in DOM. I know that for id I can use: $("#id")[0].
I use $("#id")[0] inside Developer mode in browser in order to get the element by id to see what methods (like .getText(), innerHTML or others) am I able to use for the element. I want to know how to do this by XPATH
Thanks in advance
On Firefox console you can find (and explore) the element like this:
$x("//button[#id='myButton']")
But if you want to call a function on the element, you have to call it like this:
$x("//button[#id='myButton']")[0].click()
because there is always an array of elements returned (provided that the element is present and the first one in the array).
Thanks for the comments. I found out the answer. It can be done with:
$x("xpath")

How do get id & text from a dojo/dijit combobox?

How do you get the id for the underlying record in a combobox with dojo? I've tried altering the following sample but all I can get is the text shown to the user (the "name" property of each array element), and NOT the state's two-letter abbreviation ("id" property).
http://dojotoolkit.org/reference-guide/1.10/dijit/form/ComboBox.html#id3
thanks. PS, I'm a total noob with dojo. This is my first experiment with it.
It's like #CodeCrisis said, you can get the item by using dijit.byId('stateSelect').item. Though I'm not really pleased with how outdated the docs are (the dijit.byId()command is deprecated), this is a proper 1.10 solution:
query("button").on('click', function() {
console.log(comboBox.get('item'));
});
This will return the selected record in the store (containing both the id and name properties in this case).
A full, working example can be found here: http://jsfiddle.net/zt6xggL8/
How about this...
base on the link given
http://dojotoolkit.org/reference-guide/1.10/dijit/form/ComboBox.html#id3
change this onclick function
onClick="alert(dijit.byId('stateSelect').get('value'))"
into
onClick="alert(dijit.byId('stateSelect').item.id)"
i know this is not the best answer but i hope this will help you..
check out the answer of Philippe
Dojo : ComboBox selected and show data id
hehehe... :)
just passing by...
Not the prettiest but this works. Basically need to query the 'store' variable for the name that is being displayed when you just use 'value'. [0] denotes the first object in the store list, so if you have multiples with the same name, you'll need to use the foreach() function.
<button onClick="var namevar=dijit.byId('stateSelect').get('value');
alert(dijit.byId('stateSelect').get('store').query({name:namevar})[0].id);">
Get value
</button>

ng-model - combine dynamic and static

Is it possible to append a static subfield to a dynamic ng-model identifier?
ng-model="newDevice[field.name].min"
ng-model="newDevice[field.name].max"
I need the "min" and the "max" as static fields. But the example did'nt work.
thanks!
Seems to work for me. You can take a look at this plunker and see the difference from your example:
http://plnkr.co/edit/0a2TFf7GtAz8VN0JTUin?p=preview
Or you can provide a plunker of your own that shows your problem.
Update: In order to use newDevice[field.name].min, you must have an object on your scope called new device, and one of its properties must be the value of field.name. Otherwise you can't use ng-model as is.

Selenium CSS selector by id AND multiple classes

Using selenium for the first time here, I was wondering why:
final WebElement justAnId = findElement(By.cssSelector("#someId"));
final WebElement whatIWant = justAnId.findElement(
By.cssSelector(".aClass.andAnother input[type=text]")
);
works, but not:
final WebElement whatIWant = findElement(By.cssSelector(
"div#someId.aClass.andAnother input[type=text]"
));
Although they seem equivalent to me I get:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"css selector","selector":"div#someId.aClass.andAnother input[type=text]"}
Is this intended behaviour or a bug in Selenium? I had a quick look in the bug tracker in Selenium but I didn't see anything about that. I wanted to ask here before raising an issue that doesn't need to be. Also as far as I understand it doesn't work in IE6 but who cares. I was using firefox for this run.
Actually the two are quite different selectors.
Here is your cssSelector:
div#someId.aClass.andAnother input[type=text]
But what you really wanted to write was:
div#someId .aClass.andAnother input[type=text]
notice the space between ID and class. you need that.
findElement() finds an element in the current context, which means your first snippet of code is really finding an element that matches .aClass.andAnother input[type=text], which is contained within #someId. The element with that ID may or may not contain the two classes; WebDriver doesn't assume you're referring to the same element; it just finds the input as long as its ancestors are #someId and .aClass.andAnother.
This is completely different from div#someId.aClass.andAnother input[type=text], which finds any input[type=text] within div#someId.aClass.andAnother only (i.e. it's a div that contains both the ID and the classes).

Resources