Just started studying Selenium. I have a problem. Сan't get text. getText() returns empty string. getAttribute() returns null.
The getText() is used on a HTML element that looks like this:
first element:
<span _ngcontent-enu-c116="" class="line-chart-tab-value ng-star-inserted"> 2 807,60 $ </span>
second element:
<span _ngcontent-enu-c116="" class="line-chart-tab-value ng-star-inserted"> 200,00 $ </span>
find by xpath:
WebElement sale = (By.xpath("//label[1]//span[#class='line-chart-tab-value ng-star-inserted']")));
System.out.println("Sale=" + sale.getText());
WebElement saleReturn= driver.findElement((By.xpath("//label[2]//span[#class='line-chart-tab-value ng-star-inserted']")));
System.out.println("Return=" + saleReturn.getText());
I would be grateful for any help.
List<WebElement> Ele = driver.findElements(By.xpath("//span[#class='line-chart-tab-value ng-star-inserted']"));
for(int i=0;i<Ele.size();i++) {
System.out.println("Sale "+Ele.get(0).getText());
System.out.println("Return "+Ele.get(1).getText());
}
Related
I need to know whether we can find the existence of a pseudo element like ::after and ::before
My aim is just to return true or false if it is present.
However it cannot be done using:
browser.driver.findElements(by.id('id')).size != 0
or
return !driver.findElements(by).isEmpty();
becasue they are psuedo elements and cannot be located through any CS or XPATH locators
Here is my HTML having ::after
<div class="parent-class">
<span class="child-class">Archive
::after
</span>
::after
</div>
Here is my HTML without having ::after
<div class="parent-class">
<span class="child-class">Archive
::after
</span>
</div>
Note: I need to verify only the ::after in DIV tag but not in SPAN tag
Yes, the pseudo-elements cannot be located by paths or CSS locators.
However, as an alternative, you can extract the inner HTML of the parent element and validate if it contains the "::after" text.
There are two ways of doing this. For the above scenario,
WebElement element = browser.driver.findElement(By.className('parent-class'))
String innerHTMLText = element.getAttribute('innerHTML');
if (innerHTMLText.contains("::after")){
// Bingo !!
}
Or else
WebElement element = browser.driver.findElement(By.className('parent-class'))
JavascriptExecutor js = (JavascriptExecutor)driver;
String innerHTMLText = js.executeScript("return arguments[0].innerHTML;", element);
if (innerHTMLText.contains("::after")){
// Bingo !!
}
EDIT 1
If you need to verify if only the div tag is having the pseudo-element you can get the span tag's HTML, get parent tag's HTML, remove span tag inner HTML from parent tag's HTML. Verify.
String divHTMLText = browser.driver.findElement(By.className('parent-class')).getAttribute('innerHTML');
String spanHTMLText = browser.driver.findElement(By.className('child-class')).getAttribute('innerHTML');
// replace all the whitespaces first for good measure
divHTMLText = divHTMLText.replaceAll("\\s+","")
spanHTMLText = spanHTMLText.replaceAll("\\s+","")
// replace the child html from parent's html with empty. which leaves us with the parent html code.
String divOnlyHTML = divHTMLText.replace(spanHTMLText, "");
if (divOnlyHTML.contains("::after")){
// Bingo !!
}
i am trying to display two "text text-pass" from html in chrome browser to my print console, apparently, it did not work, any advise please?
my browser html code
<a href="/abc/123" class="active">
<div class="sidebar-text">
<span class="text text-pass"> </span> </a>
<a href="/abc/1234" class="active">
<div class="sidebar-text">
<span class="text text-pass"> </span> </a>
My code
String 123= driver.findElement(By.xpath("//*[#id="js-app"]/div/div/div[2]/div[1]/div/div/ul/li[5]/a")).getText();
System.out.println(123);
String 1234= driver.findElement(By.xpath("//*[#id="js-app"]/div/div/div[2]/div[1]/div/div/ul/li[5]/a")).getText();
System.out.println(1234);
You can use .findElements to get multiple elements with the same pattern, it will return a list collection.
UPDATE
Refers to your comment, you need put the string into a list again and check with the Collection.contains() method:
List<String> results = new ArrayList<>();
List<WebElement> elements = driver.findElements(By.xpath("//div[#class='sidebar-text']//span"));
for(WebElement element: elements) {
String attr = element.getAttribute("class");
results.add(attr);
System.out.println(attr);
}
if(results.contains("text text-fail")) {
System.out.println("this is list contains 'text text-fail'");
}
Try this Code :
String pass = driver.findElement(By.xpath("//*[#class='sidebar-text']/span")).getAttribute("class");
System.out.println(pass);
HTML code is as follows
<div class="a-row">
<a class="a-link-normal" title="1.0 out of 5 stars" href="/gp/customer-reviews/RBDVABUKMPJY8/ref=cm_cr_arp_d_rvw_ttl?ie=UTF8&ASIN=B071NZZHF9">
<i class="a-icon a-icon-star a-star-1 review-rating" data-hook="review-star-rating">
<span class="a-icon-alt">1.0 out of 5 stars</span>
</i>
</a>
<span class="a-letter-space"/>
<a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/RBDVABUKMPJY8/ref=cm_cr_arp_d_rvw_ttl?ie=UTF8&ASIN=B071NZZHF9">One Star</a>
</div>
I want to call 1.0 out of 5 stars in the span class using the parent class='a-row'.
Can someone help on how can we call as it has to be called using partiallinktext method using only partiallinktext= out of 5 stars.
The way through which i got the output is as follows:
List<WebElement> rstar = dr.findElements(By.xpath("//*[#id='cm_cr-review_list']//div[#class='a-row']//a[#class='a-link-normal']//i"));
String c;
for(WebElement erstar : rstar) {
c=erstar.getAttribute("innerText");
System.out.println(c);
Thanks for the help #AliAzam :)
First look for the Span elem like:
WebElement spanElem = driver.findElement(By.className("a-row")).findElement(By.tagName("span"));
Then use below:
spanElem.findElement(By.partialLinkText("out of 5 stars"));
Another Solution:
List<WebElement> allParents = driver.findElements(By.className("a-row"));
for (WebElement elem : allParents) {
WebElement spanElem = elem.findElement(By.tagName("span"));
//System.out.println(spanElem.getText());
spanElem.findElement(By.partialLinkText("out of 5 stars"));
//System.out.println(spanElem.findElement(By.partialLinkText("out of 5 stars")).getText());
}
OR
List<WebElement> rstar = dr.findElements(By.className("a-row"));
for(WebElement erstar : rstar)
{
erstar.findElement(By.partialLinkText("out of 5 stars"));
String c = erstar.getText();
System.out.println(c);
}
Hopefully it resolves your issues.
I have a span tag like this:
<div class="row-flex">
<span class="icon-arrow-down-outline rotate-v"></span>
<span class="bmi-value ng-binding">22</span>
</div>
I want to get the value 22 of this span tag, so I write java code like this:
WebElement spanValue = webDriver.findElement(By.xpath("//span[#class='bmi-value ng-binding']"));
String value = spanValue.getText();
but it always returns an empty string. What is missing?
Maybe there are several span elements with the same class on your site,
you can try to be more specific like this:
WebElement spanValue = webDriver.findElement(By.xpath("//div[#class='row-flex']/span[#class='bmi-value ng-binding']"));
String value = spanValue.getText();
I have HTML code like :
<div class="ex1">
<div class="ex2">
<span>test1</span>
<span class="ex3">test2</span>
</div>
<div class="ex2">
<span>test3</span>
<span class="ex3">test2</span>
</div>
</div>
I'm using Selenium Webdriver.
And I need to create Java code which could:
If <span>test3 then select a <span class="ex3"> which located inside the same div class="ex2"
But since I have div's and spans with the same className inside one main I can't differ this spans..
Could you help me please with this issue?
So,something like this:
If <span>test3 then <span class=ex3>test2.
or
If <span>test1 then <span class=ex3>test2.
Thanks
1- Use this xpath to get to <span>test1 then <span class=ex3>test2:
//span[.='test1']/following-sibling::span[.='test2']
And, use in code like this:
WebElement ele = driver.findElement(By.xpath("//span[.='test1']/following-sibling::span[.='test2']"));
2-And, Use this xpath to get to <span>test3 then <span class=ex3>test2:
//span[.='test3']/following-sibling::span[.='test2']
Use in code like this:
WebElement ele = driver.findElement(By.xpath("//span[.='test3']/following-sibling::span[.='test2']"));
I have implemented the If statement as per your question in practice you can modify it as per your requirement.
WebElement test1_class1 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ex1']//div[1]//span[1]")));
String test1= test1_class1.getText();
WebElement test2_class1 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ex1']//div[1]//span[2]")));
String test2_1 =test2_class1.getText();
WebElement test3_class2 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ex1']//div[2]//span[1]")));
String test3 =test3_class2.getText();
WebElement test2_class2 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ex1']//div[2]//span[2]")));
String test2_2 =test3_class2.getText();
try
{
if(test1.equals("test1"))
{
System.out.println(test2_1);
}
if(test3.equals("test3"))
else
{
System.out.println(test2_2);
}
}
catch(Throwable e)
{
System.out.println("Exception in program"+e);
}