I am having trouble in selecting a selector when I am trying to select it as a 'css-selector'
I have this selector:
<div role="button" class="jss300 jss299" tabindex="-1">
<span class="jss313">system-all</span></div>
</div>
and I am trying to get the css-selector from it, I tried this way:
"div[class~='system-paloaltonetworks']"
and my need is to get the text from the selector, in this case I want to get "system-paloaltonetworks" into string variable.
hope now the question is clear.
"system-paloaltonetworks" is the element text, not the class attribute (the class is jss313). You can't locate it with cssSelector you need to use xpath (you should also notice the element has span tag, not div)
driver.findElement(By.xpath("//span[text()='system-paloaltonetworks']"));
You are using class~= but aren't comparing with the class...
You try: driver.findElement(By.xpath("//*[#class='jss313']"));
Related
I'm trying to find an element by the text it contains, then check that that element also has a link to a particular place. I'm using selenium/java.
I'm trying to find elements by text when I can to minimise how many changes I will need to make if the UI is updated (reduce test maintenance costs).
I've tried the following, but the assert fails as the getAttribute ends up being null.
WebElement newsHeadlineTemplate = driver.findElement(By.xpath("//*[contains(text(), 'News Headline')]"));
Assert.assertEquals("Template not clickable", "/news/create/new", newsHeadlineTemplate.getAttribute("href"));
HTML for element I'm trying to find/use:
<div class="columns">
<div class="column is-one-third">
<p>News Headline</p>
</div>
</div>
I'm still fairly new to selenium so any help is very much appreciated.
Your XPath selector is a little bit wrong, you're matching <p> tag and you need to match the <a> tag which is the following-sibling for the <p> tag.
So you need to amend your expression to look like:
//p[text()='News Headline']/following-sibling::a
More information:
XPath Tutorial
XPath Axes
XPath Operators & Functions
I would like to retrieve a webelement out of a nested html path using either css selectors or xpath. My specific use case is that I would like to select the i element in the following snippet:
<td class="headerActionsTd" data-rolename="Speaker">
<div class="headerActions">
<span class="addNewParticipantSection">
<i class="icon fa fa-user-plus" title="Add New"></i>
</span>
How do I obtain the i webelement for this using either css selector or xpath?
Use this xpath: //td[#data-rolename='Speaker']//div//span//i[#title='Add New'] or
css : div.headerActions i
driver.findElement(By.cssSelector("div.headerActions i"));
for multiple elements :
List<WebElement> users = driver.findElements(By.xpath("//td[#data-rolename='Speaker']//div//span//i[#title='Add New']"));
A simple one would be this :
CSS_SELECTOR
i.icon.fa.fa-user-plus[title='Add New']
Note that, if there is multiple element with this css selector, then you have this facility to differentiate between them:
:first-child
:nth-child(n)
:nth-last-child(n)
More can be found at this link
XPATH : would be :
//td[#data-rolename='Speaker']/descendant::span[#class='addNewParticipantSection']/i
Hope that helps.
Since you said data-role only changes in the comment of the other person, here is the xpath you can use
"//td[#data-role='Speaker']//i"
Or
"//td[#data-role='Speaker']/div/span/i"
Another option is that you can attempt to locate elements inside other elements. For example, you could store the addNewParticipantSection div in a WebElement, then use that WebElement's findElement method to locate the i element within it. Like so:
WebElement section = driver.findElement(By.className("addNewParticipantSection"));
WebElement icon = section.findElement(By.tagName("i"));
icon.click();
My xpath is
//div[contains(#class,'ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable')]//div[#class='ui-widget-content slick-row odd' or #class='ui-widget-content slick-row even']/div/input[contains(#id,'custProglabel')]
I need to access div just before input tag and get its text. I tried using:
By.Xpath(//input[contains(#id,'custProglabel')]/preceding-sibling::div).getText();
I have many elements like this and only the content of my input tag changes. So based on the content I am trying to access the immediate div and get its text.
You can get immediate parent using ..
By.xpath("//input[contains(#id,'custProglabel')]/..")
As a side note, xpath should start with lower case 'x'.
Rather than drop down to input and come back up to div, consider
using a predicate on div
//div[input/#id='custProglabel']
or any element
//*[input/#id='custProglabel']
Also, note that contains() could mistakenly match custProglabel2 or MYcustProglabel, so it's better to use equality test here.
use below xpath:
//div[contains(#class,'ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable')]//div[#class='ui-widget-content slick-row odd' or #class='ui-widget-content slick-row even']/div/input[contains(#id,'custProglabel')]/parent::div
<div class="col-lg-3 col-sm-4 col-xs-6">
<div class="logo">..................</div> </div>
logo is inner div.
I need to acces col-lg-3 col... div content using xpath.
I did this in java:
WebElement mydiv = driver.findElement(By.className("col-lg-3 ")) ;
but it does not work - because it has spaces in the name.
How do I access this using xpath?
In CSS, you could access it like this:
driver.findElement(By.cssSelector(".col-lg-3 .logo"));
Since you asked, in XPath, it could be:
driver.findElement(By.xpath("//div[contains(#class, 'col-lg-3')]/div[contains(#class, 'logo')]")
As you can see, the CSS selector is much simpler.
To see xpath, I suggest to install Firebug and FirePath; those extensions are Firefox.
Example:
Can you see the image? xpath
Imagine that you don't have id neither name locators or you need to use xpath
Open Firefox.
Click on "Firebug". (#1)
There a panel.
Click on "Click an element in the page to inspect" icon (#2)
Click on "FirePath" tab. (#3)
Select the element that you wish (#4)
It displays the xpath into textbox. (#5)
So, you need to add this line:
driver.findElement(By.xpath("html/body/form/fieldset/p[1]/input"));
I have multiple div's in a webpage URL that I have to parse which have the same class name but different names with no id's.
for eg.
<div class="answer" style="display: block;" name="yyy" oldblock="block" jQuery1317140119108="11">
and
<div class="answer" style="display: block;" name="xxx" oldblock="block" jQuery1317140119108="11">
I want to select data and parse from only one of the div's say namely (name="yyy") (the content inside the div's are <href> links which differ for each class.
I've looked up the selector syntax in the Jsoup webpage but can't get a way to work around it. Can you please help me with this or let me know if I'm missing something?
Use the [attributename=attributevalue] selector.
Elements xxxDivs = document.select("div.answer[name=xxx]");
// ...
Elements yyyDivs = document.select("div.answer[name=yyy]");
// ...