Selenium Webdriver (Java) - selecting specific element depending on the condition - java

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

Related

How to check if a pseudo element exists or not through selenium

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 !!
}

how to display span class field

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

How to call a element by partialinktext using its parent class in selenium webdriver

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.

How to get input value by using class?

I am using the code below
WebElement inputele = driver.findElement(By.className("class_name"));
String inputeleval = inputele.getAttribute("value");
System.out.println(inputeleval);
but the value is empty. The HTML is below.
<div id="main">
<div id="hiddenresult">
<div class="tech-blog-list">
<label for="Question">1st Question</label>
<input id="txt60" class="form-control" type="text" value="sddf sd sdfsdf sdf sdfsdf sdfsdfsd fsd" />
</div>
</div>
<div class="pagination_main pull-left">
<div id="Pagination">
<div class="pagination">
<a class="previous" onclick="PreviousBtnClickEvent();" href="javascript:void(0)">Previous</a>
<a id="pg59" class="ep" onclick="PaginationBtnClickEvent(this);" href="javascript:void(0)" name="Textbox">1</a>
<a id="pg41" class="ep" onclick="PaginationBtnClickEvent(this);" href="javascript:void(0)" name="Textbox">2</a>
<a id="pg40" class="ep" onclick="PaginationBtnClickEvent(this);" href="javascript:void(0)" name="Textarea">3</a>
<a id="pg60" class="ep current" onclick="PaginationBtnClickEvent(this);" href="javascript:void(0)" name="Textbox">4</a>
</div>
</div>
</div>
</div>
Try using WebDriverWait to wait until element fully loaded on page and visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement inputele= wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("class_name")));
String inputeleval = inputele.getAttribute("value");
System.out.println(inputeleval);
Note :-By.className("class_name") will give that element which class attribute equal to class_name. Make sure which element you want to locate is unique element with class attribute equal to class_name otherwise wise it will give first element with condition true.
Hope it will work..:)
Looks like your code is pretty close but you have the wrong class name? In your code above, you had "class_name" instead of "form-control". I'm assuming that was some sample code and not the actual code you are using? There is only one INPUT in the HTML and the code below should work. It also has an ID so that should be more specific in case there are more than one INPUTs on the page.
WebElement inputele= driver.findElement(By.className("form-control"));
String inputeleval = inputele.getAttribute("value");
System.out.println(inputeleval);

Unable to trace an element by attribute or text

I am having trouble with clicking at an element of a menu which is written like this:
<div class="menu">
<ul class="tabs ctrlTabsProfile">
<li class="active" data-tab="tabDetail">User Details</li>
<li data-tab="tabEmail">Email</li>
<li data-tab="tabPass">Change password</li>
<li data-tab="tabAdress">Account Details</li>
</ul>
</div>
I have tried these:
driver.findElement(By.linkText("Account Details")).click();
driver.findElement(By.cssSelector("li[data-tab=tabAdress")).click();
driver.findElement(By.xpath("li[data-tab='tabAdress']")).click();
also tried listing the elements but got null only :
for(WebElement el : driver.findElements(By.cssSelector(".tabs.ctrlTabsProfile"))) {
try {
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Account Details[\\s\\S]*$"));
} catch (Error e) {
System.out.println("Not found: \"Account Details\".");
}
String s = el.getAttribute("data-tab");
System.out.println(s);
if(s.equals("tabAdress")) {
driver.findElement(By.xpath("li[data-tab='tabAdress']")).click();
}
}
Solutions? Sugestions? Errors?
Well, for one, your xpath selector is incorrect.
driver.findElement(By.xpath("li[data-tab='tabAdress']")).click();
should be:
driver.findElement(By.xpath("//li[#data-tab='tabAdress']")).click();
edit:
And your css selector is incorrect as well.
driver.findElement(By.cssSelector("li[data-tab=tabAdress")).click();
should be:
driver.findElement(By.cssSelector("li[data-tab='tabAdress']")).click();
edit #2:
and:
driver.findElement(By.linkText("Account Details")).click();
will only work if the element is a link, which in this case it is not.
Aholt is right, driver.findElements(By.cssSelector(".tabs.ctrlTabsProfile")) will return only ul elements. To access all <li>, you could try:
driver.findElements(By.cssSelector("ul.tabs.ctrlTabsProfile li.active"))

Categories