How to retrieve value from span in class - java

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.

Related

Clicking an element having same class name as others

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
}

How to inspect any element in selenium

I am trying to perform the mouse hover, where 2 elements have same class name. I want to click on second element but its clicking on first element. How to locate the second one if there is only class and href attributes given.
I've tried to create my own xpath using classname and href but its not working.
You can try using the className selector and then using index to get the "2nd" element with that class name.
For this element:
<a class="desktop-main" data-reactid="137" style="border-bottom-color:#fb56c1;" data-type="navElements" data-color="#fb56c1" data-group="women" data-index="1" href="/shop/women">Women</a>
Try this:
List<WebElement> elements = driver.findElements(By.className("desktop-main");
elements.get(1).click();

WebDriver getting an element java

I'm accessing a website with webdriver, and I want to change an element in it.
I want to know how to access an element and change it. In this case I want to add text to an element i.e. transform this:
<span data-id="tag-dist-Lisboa" title="Para eliminar uma das opções, faça duplo clique.">
</span>
Into this:
<span data-id="tag-dist-Lisboa" title="Para eliminar uma das opções, faça duplo clique.">
Some random text
</span>
My main problem has been finding the element.
I have added above some of the HTML, if you need more information please say so.
As I have already posted one answer on your previous question-
Use following code to set value for the span tag-
int textLength = driver.findElement(By.xpath("//span[#data-id='tag-dist-Lisboa']")).getText().length();
if(textLength<=0)
{
WebElement element = driver.findElement(By.xpath("//span[#data-id='tag-dist-Lisboa']"));
JavascriptExecutor js= (JavascriptExecutor)driver;
js.executeScript("arguments[0].innerText = 'Your Text Here'", element);
}
Explanation :-
Find your required span tag with data-id attribute and check if it contains text or not If yes then use that text value as per your choice.
If not then set your required text for that span tag
Let us know is it as per your question or some other you want to do ?

how to get a text string from

I am creating an automatic test for some webs and I'm using WebDriver, TestNG and code that is written in Java. On the page is shown register of categories, in parentheses is number of auctions and i need to get this number as variable, but i don't know, how.
I use the following code:
WebElement number1_1_vse = (driver.findElement(By.linkText("Vše")));
String text_vse1_1 = number1_1_vse.getText();
but output is only: "Vše" without (number of auctions)
link to the website
screenshot -> image with the number
can anyone advise me please?
With linktext you are finding the nested a which text is Vše only. You need to find the li containing the text Vše (949)
Use the following css selector to identify the element
By bycss =By.cssSelector(".list.list-categories>li:first-child");
WebElement number1_1_vse = driver.findElement(bycss );
String text_vse1_1 = number1_1_vse.getText();
WebElement parent = number1_1_vse.findElement(By.xpath(".."));
// which will give you access to <li>
List<WebElement> childs = parent.findElements(By.xpath(".//*"));
// childs.get(1) has the result.
Please try to get the xpath value and then try to search it with below mentioned syntax :
findelementbyxpath("//*[#id="post-form"]/h2")
To get xpath value of and element : right click --> inspect element --> right click copy xpath

How to get css class name using Selenium?

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");

Categories

Resources