<span class="benefit_subtitle"> - What applications are utilizing my network?</span>
This is same path is giving more than 6 matchings.. I need to select the one element from the path using only css selector
css : span.benefit_subtitle
I want to covert this xpath into css
By.xpath('(//span[#class="benefit_subtitle"])[3]')
This should work.
driver.findElement(By.cssSelector(span[class=benefit_subtitle]:nth-of-type(3)))
Sauce labs has a good page on these.
Related
I'm trying to automate process of pinging a person on a messenger on selecting the messenger name(Nayan)
Please find the Html code
<div class="Presence__text">
<span class="Presence__nameText">Nayan<span class="Presence__contactFlagIndicator"></span>
as text element present inside the span tag is unique (Nayan), i want to select based on that text element.
Problem statement :Unable to select an element using text present in span tag
I wanted to open the text of "Nayan" using xpath, can anyone help me solving this problem please.
XPath can be used to locate elements based on their text content.
Accordingly to presented here HTML the following XPath can be used:
"//span[contains(text(),'Nayan')]"
Selenium command using this XPath in Java (you didn't mention the language you are using, but accordingly to your previous questions I see you are using Java) can be as following:
driver.findElement(By.xpath("//span[contains(text(),'Nayan')]"));
In case "Nayan" text is unique it's always better to use contains rather to use equals method since web element may contain extra spaces.
I mean when possible [contains(text(),'Nayan')] is better to use than [text()='Nayan']
Also, since you are using Selenium it can be Xpath 1.0 only since Selenium not supporting Xpath 2.0 and higher, it supports XPath 1.0 only
I was using selenium IDE to automate testing on a web page that has dynamic xpath in it.
I noticed selenium IDE was capturing the xpath fine the first time playing it. Then after closing the browser and opening, of course the xpath has changed, but the target saved was the old xpath.
Is there a way to handle this in selenium?
I know I can use the .contains method but can i apply that to the target?
Picture of selenium IDE firefox extension
To identify the dynamic elements you can construct dynamic locators. As couple of examples:
Using a css for a <span> tag with id attribute starting with abc:
span[id^='abc']
Using a css for a <span> tag with class attribute containing pqr:
span[class*='pqr']
Using a xpath for a <span> tag with value attribute ending with xyz:
span[value$='xyz']
Using a xpath for a <span> tag with id attribute starting with abc:
//span[starts-with(#id, 'abc')]
Using a xpath for a <span> tag with class attribute containing pqr:
//span[contains(#class, 'pqr')]
Explanation of the dynamic CSS_SELECTOR
The wildcards are defined as follows:
^ : To indicate an attribute value starts with
* : To indicate an attribute value contains
$ : To indicate an attribute value ends with
References
You can find a couple of relevant detailed discussions in:
Java Selenium webdriver expression finding dynamic element by ccs that starts with and ends with
I am trying to locate below link by using a[text='This is a link'] and a[innertext='This is a link'] but both of them are not working.
I know this can be achieved by using XPath or other ways, but I am curious why CSS Selector that I have used is not working. refer this link.
<a title="seleniumframework" href="http://www.seleniumframework.com" target="_blank">This is a link</a>
You are trying to locate a link by using the following CssSelectors:
a[text='This is a link']
a[innertext='This is a link']
As per Issue#987 and Issue#1547:
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
You can find a detailed discussion in selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”
Solution
As per the HTML you can use either of the following solutions:
CssSelector using the attribute title:
"a[title='seleniumframework']"
CssSelector using the attribute href:
"a[href='http://www.seleniumframework.com']"
CssSelector using the attributes title and href:
"a[title='seleniumframework'][href='http://www.seleniumframework.com']"
The right CSS Selector for your case is:
a[title="seleniumframework"]
or
a[href="http://www.seleniumframework.com"]
You can also use this one: a[target="_blank"], but the ones above are more unique.
Hope it helps you!
Since you are trying to use attribute selector, you can only select from available attributes that the hyperlink element currently has.
text is not available attribute in html so selecting it this way through css will not work.Rather you should try to use other attribute like a[title=seleniumframework] for example.
So i have this element:
<span class="clas">Create new form</span>
And i want to get this elemen:
span:contains('Create new')
Why this is not working ?
Based on your selector, I assume that you are trying to use the :contains() CSS pseudo-class selector, which has been removed from the CSS3 spec. This won't work because the latest browsers conform to the new CSS standards.
If the class attribute for this specific element is unique, then you can use:
span.clas
If not, then you are forced to use xpath:
//span[contains(text(), 'Create new')]
I've been working on automation of a product that uses Dojo. The html I'm working with is very messy.. I need to click on div that has following css selector
div.dijit.dijitReset.dijitInline.dijitLeft.dijitTextBox.dijitComboBox.dijitDateTextBox.dijitValidationTextBox.dijitTextBoxError.dijitComboBoxError.dijitDateTextBoxError.dijitValidationTextBoxError.dijitError
I'm using firefinder plugin in Firefox and it can see the element all the time, out of 2 chrome plugins I have (CSS selector tester and CSS and Xpath checker) only the first one can find the element.
When I run my selenium code I get org.openqa.selenium.NoSuchElementException.
I tried selecting classes with . and with [class=".."] as well and both failed.
Is there some selenium limitation on how many classes you can have assigned to your element before it stops seeing an element? What stable approach can I use to make my tests work?
Use FirePath plugin in firefox and look for unique classes so you only have 1 unique selector. Also look up CSS selectors, they will help you in the long run
http://www.w3schools.com/cssref/css_selectors.asp