I want to get the class name inside the div element by xpath.
<div class="indicator true"></div>
And I want to check if the class nams equals to "indicator true". Any help?
You can access the class value of the div element by using the WebElement.getAttribute() - Method.
Your Problem is to get the div element without the class as part of the selector.
I suggest to add an id attribut to the div and select the div by id selector. Then you can check class value.
WebElement divElem = driver.findElement(By.id("someId"))
assert divElem.getAttribute("class").equals("indicator true");
And I want to check if the class nams equals to "indicator true". Any help?
Yes, there is help.
driver.findElement(By.xpath("//div[#class = 'indicator true']")) .click();
That said, I am not sure what your intention is. Because you also say:
I want to get the class name inside the div element by xpath.
The path expression above returns a div element, not the class name.
//*[contains(concat(" ", normalize-space(#class), " "), " foo ")]
source : Selecting a css class with xpath
you can use the getAttribute() and enter the identify name in the brackets
this will return you the identify value.
the following code should return you "indicator true":
assert divElem.getAttribute("class");
Related
I have a table on a web page that its xpath differs from time to time but its class name doesn't. so I can handle it by its class name but my code needs the xpath of that element so how to get the xpath of that element?
OR how to deal with such elements that its xpath differs from time to time?
String xpath = "here sould be the xpath of that element";
wait_page_loading(By.xpath(table_xpath)); //this other function in my class wait the page loading
WebElement Webtable = driver.findElement(By.xpath(table_xpath));
List<WebElement> totalRowCount = Webtable.findElements(By.xpath(table_xpath + "/tbody/tr"));
if(totalRowCount.size() <= 1) {
throw new Exception("Can't find results in the problem page");
}
return totalRowCount.get(0).findElements(By.xpath("td")).get(6).getText();
Thanks a lot in advance.
I see a div and then we have a child table, please use the below xpath :
//div[#class='table-responsive']/table
Below xPath returns all the row values of the table.
//div[#class='table-responsive']/table/tbody/tr
how to deal with such elements that its xpath differs from time to
time?
If a xPath value partially dynamic I would suggest you to us contains method. For an example,
<input class = "User1235">
In above xPath the value 1235 is dynamic in this case you can create a xPath with contains
//input[contains(#class,'User')]
How do I locate the input element by id where the last 2 numerical digits change randomly?
<input id="start-time-hours-c31" class="focused" name="startTimeHours" value="" placeholder="hh" maxlength="2" type="text">
Quick answer
When last 2 numerical digits change randomly,
driver.findElement(By.xpath("//input[contains(#id,'start-time-hours-c')]"));
Using 2 attributes of Input tag,
driver.findElement(By.xpath("//input[#name='startTimeHours' and #class='focused']"));
Please let us know if it resolved your query.
As the last 2 characters of the id attribute changes randomly, additionally you need to consider some other attributes as well and you can use either of the following Locator Strategies:
cssSelector:
WebElement elem = driver.findElement(By.cssSelector("input[id^='start-time-hours-c'][name='startTimeHours']"));
xpath:
WebElement elem = driver.findElement(By.xpath("//input[starts-with(#id, 'start-time-hours-c') and #name='startTimeHours']"));
As the html also has a name tag you can find the element by using that name tag, like:
WebElement element = driver.findElement(By.name("startTimeHours"));
And if the name tag startTimeHours is not unique on the page, then you can find the element by using the below xpath:
WebElement element = driver.findElement(By.xpath("//input[contains(#id,'start-time-hours')]"));
Agreed with #TheSociety answer, you can also be used as the combination of name and id
//input[contains(#id,'start-time-hours-c') and contains(#name,'startTimeHours')]
Use contains, sibling or Ancestor when the value is dynamic.
xpath - //input[contsins(#id,'start-time-hours-c')]
You can use either of them:
//input[starts-with(#id,'start-time-hours-c')]
(or)
//input[contains(#id,'start-time-hours-c')]
if the above returns a single webelement then you can use it or else if it returns multiple elements go for name attribute in the xpath.
I want to click on hospital panel. The contains a class attribute and inner text and i need to click on "后三".
I get an error saying "unable to locate element". What am I doing wrong?
HTML:
<span class="tab-back" value="2" tag="0" default="0"><span class="tabbar-left"></span><span class="content">后三</span><span class="tabbar-right"></span></span>
CODE :
element = driver.findElement(By.xpath("//span[#class='content']/text()[last()]"));
it's seems not getting the value of 后三. Kindly advise
Use the xpath below:
WebElement element = driver.findElement(By.xpath("//span[#class='content' and contains(text(),'后三')]"));
And if multiple elements there, you can use:
List<WebElement> elements = driver.findElements(By.xpath("//span[#class='content' and contains(text(),'后三')]"));
The desired item you can access by for example:
elements.get(index)
Where index is your desired index like 0,1 etc.
I would like to select an element that matches a specific String
<img src='http://iblink.ch/resized/sjg63ngi3h3g4a.jpg' alt='tree'>
since I don't have a specific class or div to trigger I try to use getElementsContainingOwnText("resized")
method to get this element.
But it does not find it?
I also try: getElementsContainingText
Same output :(
Anyone have any idea?
The text is the part outside the tags: <tag attribute="value">Text</tag>
So you want to select Elements with a certain attribute value like this:
Elements els = doc.select("img[src*=resized]");
Have a look into CSS selectors as they are implemented in Jsoup.
I am new in selenium testing. I want to get the css class name using selenium. I am using eclipse and Java for development.
<table >
<tr class="odd"><td>Odd row</td></tr>
<tr class="even"><td>Even row</td></tr>
<tr class="odd"><td>Odd row2</td></tr>
<tr class="even"><td>Even row2</td></tr>
</table>
Is there any way to get the class name 'odd' or 'even' using selenium? I
From a WebElement you can use the getAttribute method like this:
element.getAttribute("class")
Yes, you can use getAttribute(attributeLocator) function for the your requirement.
selenium.getAttribute(//xpath#class);
Specify the Xpath of the element for which you require to know the class of.
Thanks.
There is nothing depending on eclipse or java, in fact it is more about location strategy.
If you want to access specific tr element with/without css class you can use
css locator:
css=tr:nth(indx_base_0)
nth row with class
css=tr.odd:nth(indx_base_0)
first row with class odd
css=tr.odd
Using xpath:
//tr[index_base_1]
first row with class odd
//tr[#class='odd']
nth row with class odd
//tr[#class='odd'][index_base_1]
Here are some useful examples
In context of the code snippet, say you want to get the class "odd" for the first row in the table.
You can follow the below steps:
(Note:- Assuming there is one table in your webpage)
1- Get the element first:
WebElement ele = driver.findElement(By.xpath("//table/tr[1]"));
The above code uses the xpath to get the element, i.e., the first row of the table.
2- Then, get the attribute "class" of the element using "getAttribute" method:
String class_name = ele.getAttribute("class");
The above code will fetch the "class" name of the related element and assign it to the String variable "class_name" for further use
Similarly, for getting "even" class, which is the attribute for fourth row of table , you can use the below code:
ele = driver.findElement(By.xpath("//table/tr[4]"));
class_name = ele.getAttribute("class");
If CSS is your requirement, and in the absence of additional/differentiating element attributes, try the following (in the example, I have used simple text assertions):
assertEquals(selenium.getText("css=tr.odd > td"), "Odd row");
assertEquals(selenium.getText("css=tr.even > td"), "Even row");
assertEquals(selenium.getText("//tr[3]/td"), "Odd row2");
assertEquals(selenium.getText("//tr[4]/td"), "Even row2");
Getting the CSS class name using Selenium using C#:
string _className;
IWebElement _ele = _driver.FindElement(By.Xpath("Xpath of Element"));
_className = _ele.GetAttribute("AttributeName Here");