Selenium click CSS elmenents that have same class name - css

I'm trying to click the second element where "class=uiComposerAttachment photoAttachment"

Something different from XPath, is using the nth-child locator in CSS. Note this isn't supported in IE8 and below, but other modern browsers will be fine. Example to get the 2nd element:
css=*.uiComposerAttachment:nth-child(2)

You have to find the Xpath of the class and use the Xpath then try.
click | //*[#class='uiComposerAttachment photoAttachment']
Use this in the selenium IDE
Can you provide snapshot, how you find the Xpath of the class

try this
//*[#class='uiComposerAttachment photoAttachment'][2]

If there are multiple elements with same xpath you can try below format of xpath to click the desired element.
As per your application, you have 2 elements with same xpath. So you can use the below xpath to click on the second element.
xpath=(//select[#name='listPartition'])[position()=2]

As class is same, you need to identify element as per position.
This is second element that you mentioned, so you can use
xpath=(//*[#class="uiComposerAttachment photoAttachment"])[2]

Related

how to use parent or sibling in css selenium when there is only class i.e only one attribute available and both child elements have same class name

here is the console
i have got answer using xpath but want to learn in css.
i have written xpath solution for it but kindly help with css.

why cssSelector in selenium unable to identify the text

I wanna know if i can identify the element based on text of the element, div or link using css only. Is that possible? Because i am not able to use contains method for cssselector.
Please provide the css selector syntax for the below html.
Ex- "Hi There"
No.
You can use XPATH to do it.

Selenium java - not able to identify the text box by xpath, css

Below is the HTML snippet of the textbox that i am trying to identify using chromdriver
You could use the following Xpath:
//input[#placeholder="Scan Serial No"]
ry using multiple attributes
//input[#placeholder="Scan Serial No"][contains(#class,'FC2 ELX_UserPrompt')]
For getting selectors there are a few techniques I use.
First I use the built in tools of the Chrome Developer Tools. Open these with ctr+shift+i. Then locate the element you want, ctrl+shift+c then click on the element. This highlights the element you want in the Elements tab. right click on the highlighted element and choose Copy>Copy selector. This will give you a unique css selector for that item. This works well for everything except dynamic elements that have changing id's or locations on the page.
For dynamic elements I use advanced css selectors. where you can leverage the html tag in addition to any css attributes to locate the element. Here is a decent write up on how to use these https://www.smashingmagazine.com/2009/08/taming-advanced-css-selectors/#comments
For you particular element you could do something like input[placeholder='Scan Serial No']
You can use following xpath:
name of class in xpath
//input[#class='FC2 ELX_UserPrompt binding_Screen_cc607e87_a82b_4cac_8c38_939be2ba00ff_SerialNo??']
name of class and placeholder
//input[#class='FC2 ELX_UserPrompt binding_Screen_cc607e87_a82b_4cac_8c38_939be2ba00ff_SerialNo??'][#placeholder='Scan Serial No']

selenium - how to click on a link based on its text using css instead of xpath and without using the contains operator?

I try to use css locators over xpath when I can.
I have the following xpath locator that I would like to change to css:
//table[#id='service_schedule_sets']//a[text()='OptimalTest']
I would like to use a css locator. I wish to avoid using contains as there are support issues in later versions of css (i.e. contains was removed from css2 and css3 (css2 removal was a very last minute thing) that selenium covers up but I would like to avoid.
I am trying:
css=table#service_schedule_sets a[.='OptimalTest')
but it doesn't find the element.
The HTML is:
<td>
OptimalTest
</td>
Notes:
I want to use the text in the link ('OptimalTest') not the href.
I do not want to use link=OptimalTest approach as it is not specific enough.
CSS selectors do not support node selection based on their text content.
Actually there was a suggestion for a pseudo class :contains which would suit your needs, but it was removed from CSS 3 spec.

Identify if an element has rel="nofollow" attribute using CSS for selenium tests

I am using CSS selectors as the element locators for selenium test scripts.
Now, I want to check if an element has a rel="nofollow" attribute using CSS.
Does any one know how to check this?
The question isn't 100% clear on what you're trying to do, but I'll try to answer anyway.
If I'm reading you correctly, you're working within the Selenium script language, and you want to determine your the Selenium script that the page contains an <a> element that has rel='nofollow' attribute.
According to the Selenium reference page, this should be possible, as Selenium supports most CSS selectors (the exception except pseudo selectors, but they're not relevant for you here).
It also supports DOM references and XPath references, so one way or the other you should be able to check just about anything.
For CSS, the syntax is css=cssSelector, so in your case this would be css=a[rel=nofollow].
This selector will check the page for any <a> element with the rel=nofollow attribute. If you need to check if a specific element has this attribute, then modify the selector to include the ID or class of the element you want - eg a#myspecificelement[rel=nofollow]
This would be used with a Selenium command such as assertElementPresent() to check that the element is present, or a range of other possible Selenium commands.
Hope that helps.
a[rel="nofollow"]
Won't work in all browsers.
A better solution would be to use jquery who selectors are supported in all browsers
$('a[rel="nofollow"]')

Resources