In my application id is changing dynamically,and name is not given to all elements.
Now I want to apply dynamic way of searching "x path" in different Division in HTML.
Whenever I refresh page , database value can be added or removed from page.
So is there any way for taking dynamic path of one element??
You can use custom XPath or CSSPath.
This path have some condition like "OR", "AND", etc.
You can also use some functions like contains(), text(), not(), etc.
References :
http://www.w3schools.com/xpath/xpath_functions.asp
http://www.w3schools.com/cssref/css_selectors.asp
Related
I want to select and click on the object present inside inner html (shown in the image). But the object is not getting identified. I am using Java.
Note --> my application not opening with any browser except Internet Explorer and I can't verify xpath from console/debugger so I have to verify it through code only.
Code I have tried so far but not working for me-->
option 1 -->
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a href[#text='Administrate']")).click();
option 2 -->
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a[#text='Administrate']")).click();
option 3 -->
driver.findElement(By.xpath("/html/frameset/frame[1]/html/body/ul/li/ul/li[1]")).click();
You are checking for exact text match, use contains instead
driver.findElement(By.xpath("//a[contains(text(), 'Administrate')]")).click();
Or
driver.findElement(By.xpath("//a[contains(., 'Administrate')]")).click();
Please note the difference between text() and #text
You need to change your XPath like
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a[text()='Administrate system']")).click();
Note:- Here you need to pass complete string for better learning please refer Firepath to create and evaluate your xpath
Also can use contains method to find an element on partial text basis
I write selenium tests for an AJAX application, and to make it run stable I use explicit waits. This works correctly if I use a single by to locate my element, like this:
WebDriverWait wait = new WebDriverWait(getDriver(), 50);
wait.ignoring(StaleElementReferenceException.class);
wait.ignoring(NoSuchElementException.class);
wait.ignoring(InvalidElementStateException.class);
By by = By.cssSelector("button");
wait.until(ExpectedConditions.elementToBeClickable(by));
getDriver().findElement(by).click();
However I want to wait for an element to be clickable, that is located by multiple bys, like this:
WebElement element = getDriver().findElement(By.cssSelector("form")).findElement(By.cssSelector("button"));
ExpectedConditions.elementToBeClickable(element);
The first line, where the element is found, is prone to race conditions, and does not work like if it was located with a single By. I considered concatenating the By objects in a single By, however i use both XPath and CSS selectors to locate my elements, so this is not an option. XPath is used, as I match on containing text, which is not supported by CSS3.
Is it possible to replicate the behavior of the first example, while still being able to locate the elements using multiple By objects?
You are using CSS Selectors, you shouldn't need to use multiple findElement().
You can do that all in the same selector. You should be able to do everything you can do with XPath using CSS Selectors.
I.E.
getDriver().findElement(By.cssSelector("form button"))
We can locate an element in webpage using ID, xpath, class name, tag, name, link text,attribute etc
Using which kind of locator selenium detects the element faster. Also which type of locator is good to use(faster to access and reliable)
In My Experience following is order when it comes to search time of objects.
Link Text : fastest way to search its so fast some time you have to use wait for controlling it and works same for all browsers But you need to make sure that the text content has been loaded so it will fail if your page is too much bulky
CSS path: Its Also very fast but slightly less faster then Link Text But believe me it is a nightmare to edit css paths as you won't be easily able to find if a user changed class name "xyz" to "btw"
ID/Name : ID is supposed to be unique for an element . IF your developers are following the standard way then it is best .its equal to CSS path when it comes to speed
Xpath: (Slowest of all and second problem with XPATH is every browser has Different syntax like IE it has some other type of expression when compared with FF or chrome I always avoid using Xpath) But if you have written strict xpaths then your code will work like a charm. Xpaths enhance your capability to get any elemnt and are the most widely used.
http://www.seleniumhq.org/docs/03_webdriver.jsp#locating-ui-elements-webelements
In My Experience following is order when it comes to search time of objects.
Link Text : fastest way to search its so fast some time you have to use wait for controlling it and works same for all browsers
CSS path: Its Also very fast but slightly less faster then Link Text
ID/Name : its equal to CSS path when it comes to speed
Xpath: Slowest of all and second problem with XPATH is every browser has Different syntax like IE it has some other type of expression when compared with FF or chrome
I always avoid using Xpath
I have a specific table that is within multiple tables, and does not have any unique ID or Name. I'm looking for a dynamic solution to find a specific text that will surely occur only once in the whole document, and get the WebElement of the link (text = Add) within the same row.
The text i'm looking for is in the 1st column, and the link is in the 6th column.
Basically i'm looking for a selenium alternate of nextSibling() with a twist that it has to be a link.
selenium supports XPATH which has the notions of siblings and first children etc. You can iterate but this approach makes it more robust. You can select elements too.
selenium IDE supports XPATH for finding the value or location uniquely in web page and it show notions of siblings and first children etc.
you can used Xpath of that value to find it.
i am working in selenium i want access a text field and fill with any value but that field do not have any id attribute, so tell me how to locate that field.
You can use Cssselector or xpath.
you can find lots more on google . one useful link is -
http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.html
If you cannot add id to element, then you can use other options:
use other attributes (e.g.name or any other you have) - it is not reliable, because attributes can change in time. Example:
//div[#name='some_name']
use your HTML layout to locate element - it's even less reliable, because your HTML layout may be changed in time. Example:
//footer//div[position()=2]
In order to make it easy to write your custom XPath expression I would suggest to use FireFinder for FireBug FireFox plugin. It supports both CSS and XPath expressions and makes writing expressions really straightforward.
For typing into a textbox there is no need to have an ID. You can do it with XPath or CSS.