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");
Related
I have the HTML code as:
<a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R252N8IFRXV8TW/ref=cm_cr_getr_d_rvw_ttl?ie=UTF8&ASIN=B071NZZHF9">Security concern</a>
and i have to search the element by data-hook as the class name is common for other attributes also and href and link-text are dynamic as it is changing for the next element as there are multiple elements that i need to fetch.
Can i search it by data-hook. If yes could someone help me with it!
Sure you can.
CSS:
driver.findElement(By.cssSelector("a[data-hook='review-title']"));
Xpath:
driver.findElement(By.xpath("//a[#data-hook='review-title']"));
You can also use the below code if multiple data-hook item you need.
List<WebElement> list = driver.findElements(By.className("review-title"));
for(WebElement elem : list){
System.out.println(elem.getAttribute("data-hook"));
}
As per the HTML you have shared to find the element through data-hook attribute you can use the following line of code :
driver.findElement(By.xpath("//a[#data-hook='review-title']"));
Though you mentioned class name is common it will be a good practice to keep the class name attribute in the xpath as follows :
driver.findElement(By.xpath("//a[#class='a-size-base a-link-normal review-title a-color-base a-text-bold' and #data-hook='review-title']"));
Further, though you mentioned href is changing, I will suggest to use the combination of all the three attributes to uniquely identify the element as :
driver.findElement(By.xpath("//a[#class='a-size-base a-link-normal review-title a-color-base a-text-bold' and contains(#href,'/gp/customer-reviews/') and #data-hook='review-title']"));
I found a few answers about this but they did not answer my question an so I am writing a new question.
I have HTML code having below kind of checkbox elements (in browser's inspect element)
<input role="checkbox" type="checkbox" id="jqg_TransactionFormModel501EditCollection2_147354_grid_-1274" class="cbox" name="jqg_TransactionFormModel501EditCollection2_147354_grid_-1274" value="true">
In my test case I want to click on checkbox using its ID using Selenium Webdriver.
here Id= "jqg_TransactionFormModel501EditCollection2_147354grid-1274" is dynamic.
in above id, Bold & Italic marked letters (dynamic) will change with different check boxes in same page as well as page refresh.
Bold marked letters (dynamic) will change on page refresh only (remain same through all the check boxes in same page.)
How shoud I format/write XPATH so that I can click on desired check boxes using below statement.
WebElement checkbox = webDriver.findElement(By.id("idOfTheElement"));
if (!checkbox.isSelected()) {
checkbox.click();
}
Thanks for your help in advance.. !
Here are a few examples of xpaths which you can use to find your checkbox
//input[contains(#id,'jqg_TransactionFormModel')]
OR, if you want more checks, try something like
//*[starts-with(#id,'jqg_TransactionFormModel') and contains(#id,'EditCollection2_')]
Additionally, you can try regex as well using matches
//*[matches(#id,'<regex matching your id>')]
You can use partial ID using cssSelector or xpath
webDriver.findElement(By.cssSelector("[id*='TransactionFormModel']"));
webDriver.findElement(By.xpath("//input[contains(#id, 'TransactionFormModel')]"));
You can replace TransactionFormModel with any other fixed part of the ID.
As a side note, no need to locate the element twice. You can do
WebElement checkbox = webDriver.findElement(By.id("idOfTheElement"));
if (!checkbox.isSelected()) {
checkbox.click();
}
You can write xpath like this :
//input[starts-with(#id,'jqg_TransactionFormModel')]
//input[contains(#id,'jqg_TransactionFormModel')]
I recommend not using ID's or xpath and adding a data attribute to your elements. This way, you can avoid the annoyance of xpath and have a strong selector that you feel confident will always be selectable.
For example, you could call the attribute: data-selector. You can assign a value of "transactionFormModelCheckbox". Then, when creating a new element, you create by css selector with the value referenced above.
No matter what type of formatting, you'll always be able to select that checkbox - as long as the element exists in the DOM. If you need to investigate other attributes, you can use the selector to do things like hasClass() or anything else you need.
Hope that helps !
I'm currently having troubles on locating this element with dynamic id. Here are the screenshots below.
What i have right now is the element of Variables (8) //a[contains(.,'Variables (8)')]. What i need is only the "Variables" since the number 8 is always changing.
Any thoughts on this? Any ideas will be much appreciated. Thanks
First of all 'Variables (8)' is not Id, its text. Id's are not dynamic since they represent unique identifier for the web element. This will look like this (based on your example):
<div class="field" id="fieldId">
As for your question, you can find the element by partial linked text:
driver.findElement(By.partialLinkText("Variables"));
This will give you the a element no meter what the number is.
You can try the below:
driver.findElement(By.cssSelector("a:contains('Variables')"));
If you want the word "Variables" , use the below:
String str = driver.findElement(By.cssSelector("a:contains('Variables')")).getText().split(" ")[0];
Hope this Helps....
What I understand from your question is you want to locate the <a> tag which contains text "Variables".
Try using this xpath:
//div[#class="field"]/a[contains(.,"Variables")]
This xpath will locate the <a> tag after the div tag with class name =field and Contains method with <a> tag will find the element which contains text "Variables"
try this:
String varText = driver.findElement(By.cssSelector("div.triggerFirst>div:nth-child(1)>a")).getText();
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");
I am trying to get the rating of each movie but I cant seem to use the select method in the right way. I am trying to get the 7.0 part from the webpage:
http://www.imdb.com/title/tt0800369/
<div class="star-box giga-star">
<div class="titlePageSprite star-box-giga-star"> 7.0 </div>
I am using this line in java:
Element rating = doc.select("star-box giga-star").first();
System.out.println(rating);
Thanks in advance!
You can select an element by its class using .star-box-giga-star, and use text() to get the textual content of the element.
doc.select(".star-box-giga-star").text();
Problem with your selector is that you are using ancestor child selector instead of .class or element.class like div.star-box. Notice that to use multiple class you need to use element.class1.class2 or just .class1.class2 if you don't want to specify element.
Also if you want to specify parent child relationship you will have to use > so try maybe something like
Document doc = Jsoup.connect("http://www.imdb.com/title/tt0800369/").get();
Element rating = doc
.select("div.star-box.giga-star > div.titlePageSprite.star-box-giga-star")
.first();
System.out.println(rating);
Unfortunately this will print
<div class="titlePageSprite star-box-giga-star">
7.0
</div>
so if you want to get only text contend from that element use System.out.println(rating.text());
BTW since there is only one element with class star-box-giga-star you can just use
String rating = doc.select(".star-box-giga-star").text();
as shown in Alex answer