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']"));
Related
I would like to work on a efficient way to find a list of elements in a page having id attribute ( can have any value ) using selenium.
I can do that with for loop using and getAttribute("id") != null on each element of a list of webElements find using driver.findElements(By.xpath("//body//*"); But this not time efficient.
If any one can suggest an efficient way, it would be helpful. I am using Java-Selenium.
To get a list of all the elements on the web page having an "id" attribute you can do the following:
List<WebElement> list = driver.findElements(By.xpath("//*[#id]");
Since XPath to locate element having "id" attribute is simply //*[#id]
for all the elements with id attributes we can determine them like below :
xpath :
//*[#id]
and store them in a list like this :
List<WebElement> allIDsAttribute = driver.findElements(By.xpath("//*[#id]");
and then iterate this list to get individual web elements like below :
for(WebElement ids : allIDsAttribute){
System.out.println(ids.getAttribute("id"));
}
I am trying to locate and click an element which is having same className as other elements. I am unable to differentiate that element from other to click that element. Here is the HTML code of that element:
<a href="/category/men/N-fh7rea" class="accord-header">
Men
</a>
In this code, classname is same as others elements and text "Men" is also same. So made an Xpath of this:
//a[#class='accord-header' AND contains(text(),'Men') ]
Tweak the xpath a bit and use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea']
You can get more granular and use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]
You can alos use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea'][normalize-space()='Men']
if you can't find any difference you can always count whith one, from the same objects interest you. If its for example 3rd element, save all of them as a list using findElements, and then get third element from it.
List<WebElement> elems = driver.findElements(By.xpath("//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]"));
WebElement elementThatYouLookedFor = elems.get(2);
If You need to click all of elements of this kind just use foreach loop:
List<WebElement> elems = driver.findElements(By.xpath("//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]"));
for(WebElement we : elems){
we.click(); //or any other operation
}
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();
<section class="my grid">
How can I use Jsoup to optain this element (and all sub elements)?
The following does not work (is empty):
Elements ul = doc.getElementsByClass("my grid");
Elements listGrids=new Elements
for(Element section:doc.getElementsByTag("section"))
{
if(section.absUrl("Class).equals("my grid")
listGrids.add(section);
}
I don't know why your current code doesn't work, but it could be because you have a space in your value
Niko
This answer is just for your information. This could be done even easier. Just like
Elements ul = doc.select("section.my.grid");
or for iteration as
for(Element section : doc.select("section.my.grid")){
System.out.println(section.text());
}
Explanation
Esentially you could filter tag based on class by . (DOT) selector. Refer here
For eg - el.class gives all elements with class, e.g. div.masthead selects all div tags with class masthead. So in your case you have two classes "my" and "grid" for section tag. So just filter like
Elements ul = doc.select("section.my");
or
Elements ul = doc.select("section.grid")
This will give you all section tags with a class attribute of my or grid. But in case if you have multiple combination of "my" class and you want just "my" and "grid" together do nesting.
Elements ul = doc.select("section.my.grid");
Classes are separated by a space. In your case you add 2 classes to your section ("my" and "grid").
If you want to have a readable class, use a "-" to separate them.
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");