Identifying a link in selenium (no id or class provided) - java

I would like to know how to identify via webdriver the following html "node":
thank you <em>very much indeed</em> - Angielsko-Polski Słownik <b>...</b>
(It's just any link of google when one launch a google search)
I have googled it, however I have found only cases where the id or the class were provided.
What about in this case?
This is my failing try:
webdriver.findElement(By.xpath("//a[#href='http://www.google.pl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCcQFjAA&url=http%3A%2F%2Fpl.bab.la%2Fslownik%2Fangielski-polski%2Fthank-you-very-much-indeed&ei=Sia8U6LPCevB7AagwoCICg&usg=AFQjCNF6y7swYrp3axD0hNrCWfjovhcVPw&bvm=bv.70138588,d.bGE']")).click();
Thanks in advance.

There are several possibilities:
By.tagName("a")
However, chances are there are more than one a tag, and so the above will pick the first one it encounters. To get more specific, you can use:
By.xpath("//a[0]")
0 in this case refers explicitly to the first a tag. However, to give a precise XPath answer, I would need to see more your page code, as well as your exact requirements. You can also use:
By.partialLinkText("thank you very much indeed")
This works best if you have unique enclosed text.
You may also want to read through the rest of the locators in the API.

If you are not able to identify the the link directly, you can try based on other element.
When you have any adjacent div or element having unique value, you can refer the link relative to that.
WebElemenet element = driver.findElement(By.cssSelector("div#id a"));
This will get the link element which is present in the div having an id value of "id".

Related

How to search for a modification of an element in a web page using Selenium?

I am a beginner with Cucumber/Selenium, and I have a web page on which there is a table on which one of the boxes can be 0 or 1. my problem is that the value of this box varies over time, I have tried many times to check that this element is 1 then 0 after a certain time, but I do not succeed in it.
I know how to get the css or xpath of this element, but it doesn't change when the value of this element changes, and no way to get the associated value (0 or 1) of this element.
What I've tried:
By inspecting the part of the source code associated with this element I saw that a character in the source code went from 1 to 0 at the same time as the box, and I tried to check this change with driver.getPageSource.contains ("the area around the source code that contains the changed character*"), after waiting a little with a wait instruction to allow the source code the time to change, but this has never worked.
Anyone have an idea to help me do it right? It is very important for me to find the solution to my problem,
Thank you.
If you want to get the text from an element, use:
String value = driver.findElement(By.XPath(*your XPath here*)).getText();
This will output the inner text from the element at the moment you call it. If your element doesn't have any inner text, use:
getAttribute("value")
When trying to get the value of an element, it is best practice that you specify what your element is and then try to get the value from it, rather than getting the entire page source and making sure what you expect is contained in it.
There are many resources that will help you gain an understanding of how Selenium and Cucumber work, which I would recommend having a look through before trying to build a solution.

Selenium Webdriver: is it a professional practice to use xpath?

I am testing a web app using selenium and java. I've always avoided xpath like it was a disease. Unfortunately, I got stuck on a stubborn web element buried deep inside a table unfortunately with no id or class. I tried everything and even invited my great great grand parents but nay...nothing worked, except xpath...see below.
I tried: className, name, cssSelector e.t.c. with e.g.
driver.findElement(By.className("kujes")).click();
This is what worked.
driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();
I do not want anything less than professional in my work.
So, my questions are is xpath reliable and a good practice?
Is it professional to use xpath?
driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();
The above approach is very very bad practice.
Never use indexes in your xpath. It becomes very fragile and will break every single time even when there is a small change in the target application. Try to ask the developers to add ID to that object.
It depends on the cases. Ultimate goal is to find selector which is unique and never changing until big change happens.
First you can try with id or class name which are unique.
Then we can play with css selector to find,
Element with attribute, classname , id and combination.
Element which is child of another element,
Element which is next sibling of another element.
You are using absolute xpath, which is unreadable and changing one. Using absolute xpath is completely unprofessional.
driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click()
You can use relative xpath
driver.findElement(By.xpath("//table[#id='somevalue']//td[text() = 'Name']]/preceding-sibling::td")).click()
There are few cases which are possible only with XPath in selenium
Finding parent element of an element
Finding preceding sibling of an element
Finding an element with innerText
Finding nth element of the locator
The above cases are not possible with css selector and xpath is the only straight forward way to find those element.You can also achieve these indirectly with jquery selector and javascript executor.

Appium - find element by Xpath

I have one issue which I can't resolve...
I'm trying to write automated tests with Appium...
We don't have unique elements in our application except Text.
As example we have some element which i want to click. This element has not uniquely id "elements" (many of others elements on this page have the same id) but this element has as unique text "Track"...
As I know one chance for me it's find this element by Xpath but unfortunately I can't do this and when I run my tests i get one by one exceptions...(
My xpath is:
//*[#id='elements' and contains(text(),'Track']
What I should change in my xpath? how to build it correctly?
Thx in advance.
Seems like you have a few mistakes in your XPath, This should work:
//*[#resource-id='elements' and contains(#text,'Track')]
before you go any further with your automation project, visit this site:
http://www.software-testing-tutorials-automation.com/2015/10/ui-automator-viewer-get-android-app.html
driver.findElement(By.xpath("//*[#id='elements' and contains(text(),'Track')]")).clear();

How to handle dynamic elements using Robot Framework

I am currently working on the Robot Framework and using Selenium2Libraries to work on a Web Application. I'm working on a Form and I'm dealing with a dynamic elements which is an editable text area and drop down list..
I really hope someone would be able to guide me on how I can do this. An example of what I am doing is,
[Example element code]
input id="textfield-1237-inputEl" class="x-form-field x-form-text x-form-text-default x-form-focus x-field-form-focus x-field-default-form-focus"
data-ref="inputEl" size="1" name="textfield-1237-inputEl"
maxlength="200" role="textbox" aria-hidden="false" aria-disabled="false"
aria-readonly="false" aria-invalid="false" aria-required="false" autocomplete="off" data-componentid="textfield-1237" type="text"
Any information on this would be much appreciated. Thanks!
There are many types of Identifiers are available.you can search,If the values are dynamic you can use Xpath Identifier to find the locator.Id can be used only for the static values.
In the above case you can use Xpath as
xpath=.//*[contains(type(),'text')]
because text is static.It wont be change.
When trying to handle dynamic IDs, and elements which dont have easy UIDs about them, the best way to go around this is using Xpath.
Xpaths are basically the location of the element within the HTML. This is kind of the best way to get around the problem of not having ID readily available (My work has no IDs anywhere I can use, thus I have no choice but to use Xpaths)
Xpaths are really powerful, if used correctly. If not they are really brittle and can be a nightmare to maintain. Ill give you an example of potential Xpaths you may have to use:
Select From List By Label xpath=(//select)[2] DropDownItem1
You said that you have a drop down. Here is a potenital "look-a-like" you would see. The Xpath here is basically saying, find the 2nd drop down you find, anywhere on the entire HTML page.
Xpaths will take a while to get your head round, esspecially if you have had the luxurary of using IDs. The tools I use in order to locate and debug Xpaths are:
FireBug
Selenium IDE
I mainly use Selenium IDE now, as it is a nice tool which basically lets you "Select" an element within the HTML and it will spurt out its ID, CSS Path, Xpath, DOM, etc... Not only that, when you come to discover more complex Xpaths, there is a "Find" tools which shows you visually, where your Xpath is pointing to (or isnt, if its wrong)
Something which really helped me was This. It is really usful and has a lot of examples for you to work against.
If you have any problems, just reply and ill try to help
More Examples:
Click Element //span[contains(text(), 'Submit')]
Input Text xpath=(//textarea)[3] Some Random Text!
As with the other answers, I propose that you use Xpath.
Using Xpath can point you to the element by identifying the relationship of that element with the other elements around it. So my suggestion is to find a static element that you could use as your starting point.
For example:
starting point has static id:
xpath=//td[#id='startingPoint']/following-sibling::select[1]
starting point has no id but has static text (usually the label of the field):
xpath=//td[contains(text(),'Field Label')]/following-sibling::select[1]
If you could give us an idea of what the element is..we could provide you better examples..
What I did was alter the Xpath for example:
//*[#id="cec9efb093e14182b361766c26fd1919"]/section/div[1]/ticket/div/div/input
And took out the Id what was being generated dynamically cec9efb093e14182b361766c26fd1919 to switch for an autoId I set to the parent element where the Id was being generated. It's a cheap fix but it works if only one of the parent element is being generated.
So the parent element has the attribute autoid=container added to it and I referenced it as [#autoid="container"]/section/div[1]/ticket/div/div/input in the robot code

GWT id element is changing every time in selenium

selenium.click("gwt-uid-204"); // this is recorded from Selenium IDE
I am clicking the check box in my (gwt) java application. The gwt-uid is changing ever time, so if the id changed then my element is not found in my apps. The regular expression is not working for me and I am not sure what I am doing wrong. Thanks for your help
selenium.click("gwt-uid-[0-9]);
I am using selenium 1.0.3, Java
Many GWT elements comes with ensureDebugId (method on UIObject) to allow you to explicitly set Ids to elements for testing and debugging purpose. You also need to inherit the module
<inherits name="com.google.gwt.user.Debug"/>
to make it work. The advantage of this is, you can remove the trace from the production deployment by removing the inherited module in during prod mode compile. Hence there wont be code changes to remove unnecessary Ids.
You can do it in 3/4 ways
Can check this link :
3 ways of dealing with GWT dynamic element Ids
which talks about 3 different ways of assigning a static id to your GWT elements.
Also,
You can write a custom javascript method which will fetch all the ids dynamically. Then you can process those ids for selenium actions.
There are two possible solutions. The first is to tell Se that you are using a regex by saying regex:gwt-uid-[0-9]. As you have it there it is looking the and element whose name or id is that literal string.
The other solution is to turn on static id's for things which I discuss in http://element34.ca/blog/google-web-toolkit-and-id.
-adam
Assuming you have dynamic IDs, as you have presented, first realize that Selenium's click method takes a locator argument. A simple approach is to specify a locator that finds an ID starting with your constant "gwt-uid-" prefix. You can use any of these locators as the argument to your click method, depending on your preference of technologies:
== XPath ==
//input[starts-with(#id, 'gwt-uid-')]
== CSS ==
css=input[id^='gwt-uid-']
== DOM ==
dom=for each (e in document.getElementsByTagName('input')) if (e.id && (e.id.substr(0, 'gwt-uid-'.length) === 'gwt-uid-')) e
Footnote 1: I have not used GWT, so my examples above assume that it still puts a check box in an <input> element; adjust as needed.
Footnote 2: Selenium does offer regular expression support, as Adam intimated, but there are two issues with it in this case: (1) the prefix is "regexp:" rather than "regex:". (2) Selenium's click method does not support the regexp prefix at all! (My empirical evidence suggests that locators do not use regular expressions in Selenium, only text matching arguments do.)
You can also use Firebug add ons to remove the GWT UID.
Right click where the GWT UID is and select "inspect element with
firebug" -
Click on the code where the GWT UID is and when the firebug windows
appears select "deleted attributes id"
After removing the id, right click one more time and copy the "Xpath"
Add an extra (/) and paste the xpath on the target.
This may also help.

Categories

Resources