In Protractor, the xpath contains[.,''] works all time instead of CSS's contains() feature; is there any cause for such broken functionality? or any tweak to implement it?
The pseudo class contains is not a CSS feature. It's implemented by external libraries like JQuery or Mootools. To get an element by text, use the locator by.cssContainingText:
var elem = element(by.cssContainingText('div.left', 'my text'));
Related
I my tests I am using WebDriverIO. I want to get all elements with class form_error that are visible.
I try something like this:
$$('.form_error:visible')
but the :visible filter is not a valid webdriverio selector construct.
How to achieve that?
WebdriverIO uses CSS selectors to find elements, and :visible is not a valid CSS selector.
Instead, you could go two ways:
Get all elements with a class of .form_error, then run isVisible() on them and filter the hidden ones out
Use a custom execute function to run JavaScript on the page to check for visibility of elements. Something similar to this answer.
I have a scenario in selenium where I have a web element which is available on the page but to reach out to that web element sometimes we have to scroll down depending upon some business logic.
I don't want to use javascript executor and css selector. Is there any other way we can check if element is not clickable I can try to see if there is a scroll down scroller available? And my driver should scroll down and try to check that element once before it actually throw exception.
Let me try to answer your Questions one by one.
we have to scroll down : When you think of scroll its worth mentioning that scroll method is not a method of Webdriver, this is a method of JavaScript. JavascriptExecutor is an Interface which is available in package org.openqa.selenium.JavascriptExecutor. Inside this Interface we have a method called executeScript() – so whatever script you will pass as a string it will be executed by JavascriptExecutor.
I don't want to use javascript executor : We are not using anything separate stuff by the name JavascriptExecutor. We are using the same instance of WebDriver i.e. driver, but we need to downcast it so that it can act as a JavascriptExecutor to perform whatever script you will pass as a string.
css selector : Using css selector is not mandatory. Selenium provides you the flexibility to use any of the properties of a particular element available like id/name/linkText/xpath.
Let me know if this answers your Question.
I am trying to learn Selenium and am trying to write the xpath locator for the "About Us" link on the web page - www.hdfc.com
I can do it with link as:
link=About Us
I have tried the following and it works fine:
xpath=//a[text()='About Us']
but I wanted to write the locator using xpath axes so that its flexible enough. Can someone please point me in the right direction?
In the case you have put forward the best selector you could use is ID, this is because IDs (much like classnames etc) are not dependent on the structure of the document at all but more about the content or purpose of the element. in this case you would want something like:
driver.findElements(By.Id("ic-aboutUs");
Another thing you should be aware of in general is that xpath expressions are considered a worse way to identify your elements than the use of CSS selectors, especially if you are testing in IE as the xpath implementation there is not native and is very slow. I suggest reading over http://saucelabs.com/resources/selenium/css-selectors for a brief look at some examples and also maybe have aread of http://saucelabs.com/resources/selenium/selenium-xpath-marks-the-spot in order to see some of the negatives of using xpath.
Use the below xpaths to detect 'About Us'link in your web page
Below xpath was written by refering the immediate parent node
//li[#class='expanded']/child::span[text()='About Us']
This xpath was written by using the parent node of Menu bar(parent of whole menu items)
//ul[#class='menu hdfc-investor']/child::li[contains(.,'About Us')]
How to change this below Xpath to css? Please help.
//button[text()='Continue' and #class='buttonLargeAlt' and #type='submit']
Unfortunately, you can't.
The problem is that CSS selectors can't find by text. Therefore, you can't translate text()='Continue' XPath to a working CSS selector. This is one of the two main reasons for XPaths to be actually used till today for HTML elements selecting.
There was a :contains() pseudo class for this in CSS3, but it's long gone. Sizzle, the JS engine for CSS selecting in Selenium, has kept it, though. So if your browser doesn't support native CSS selecting (or you disable it), you can use it like this:
button.buttonLargeAlt:contains('Continue')[type='submit']
I normally use this cssify, This is pretty cool
I have a web page with a form and has a field that uses the jquery autocomplete function.
This is how the HTML renders after a user name returns 1 or more results.
However I cannot figure out how to make Selenium "click" a result.
Can I do a jQuery type of selector.
e.g.
$(".ul.ui-autocomplete li:first a")
Use XPath selector in Selenium:
xpath=//li[contains(#class, 'ui-autocomplete')]/li[1]/a
not checked, might require some corrections.
in response to "Can I do a jQuery type of selector," jQuery uses CSS selectors. Selenium can also use CSS selectors; just prefix the selector with "css=". so:
css=.ul.ui-autocomplete li:first a
Next way to use xpath like this
xpath=/html/body/ul[2]/li[1]/a
Suppose you have a dynamic XPATH then you can point to an element like this
driver.findElement(By.className(""));