Selenium skip html element with hidden attribute - java

I'm using selenium with java and I'm trying to test the search scenario. After I perform the search the html elements which do not suite the search keyword are hidden with hidden attribute (see the example below, the first element does not match the search criteria and the second does):
<ion-item-sliding class="item-wrapper" hidden="">
<button class="item item-block item-md" ion-item="">
<p>I am hidden</p>
</button>
</ion-item-sliding>
<ion-item-sliding class="item-wrapper">
<button class="item item-block item-md" ion-item="">
<p>I am not</p>
</button>
</ion-item-sliding>
My goal is to find the text in the visible element (the second one in the example). When I use the simple selector
//button[#class='item item-block item-md']
the hidden element gets found, so I'm using the selector like this
//ion-item-sliding[#class='item-wrapper' and not
#type='hidden']//button[#class='item item-block item-md']...
but no luck. Please advice with any ideas/documentation about the selector.

What you need here is to utilize the getAttribute function. What this call does is that it returns an attribute value if its set or null otherwise.
You can use FindElements passing your selector, and iterate over the found elements and only return the one where
getAttribute("hidden") != null
Hope this helps.

Use isDisplayed() method to see if the element is visible on the browser before getting the text, like this
List <WebElement> elements = driver.findElements(By.cssSelector(".item.item-block.item-md"));
for(WebElement e:elements)
{
if(e.isDisplayed())
String text = e.getAttribute("innerText");
}

Related

How to check if located link contains a tag with a given text?

I need to check if located link contains <span class="extra-light"> with a given text.
The way I locate the link:
ExpectedConditions.elementToBeClickable(By.xpath("//a[#so-extra=\"x12fog\"]"))
How to make it?
You can use findElements method with XPath locator defining the desired element as following:
if(driver.findElements(By.xpath("//a[#so-extra='x12fog']//span[#class='extra-light' and contains(.,'stackoverflow')]")).size()>0){
System.out.println("Element found");
}
findElements method return a list of found matching elements. So, if there is such element the list will be non-empty (size>0), otherwise the returned list will be empty.
Try either of the xpath.
//a[.//span[#class='extra-light']]
Or
//a[.//span[#class='extra-light' and text()='stackoverflow']]
Or
//a[.//span[#class='extra-light' and contains(.,'stackoverflow')]]
Code should be
ExpectedConditions.elementToBeClickable(By.xpath("//a[.//span[#class='extra-light' and text()='stackoverflow']]"))
Or
ExpectedConditions.elementToBeClickable(By.xpath("//a[.//span[#class='extra-light' and contains(.,'stackoverflow')]]"))
or
ExpectedConditions.elementToBeClickable(By.xpath("//a[.//span[#class='extra-light']]"))

How would I select this element using Selenium in Java?

I am trying to select for the value 1352 in Java Selenium on ChromeDriver
<span class="numfound" id="yui_3_18_1_1_1522936314968_15">1352</span>
Because the id is nonintuitive, I'd like to select using the String "numfound". I've tried selecting byClassName("numfound") and this was returned:
<[[ChromeDriver: chrome on MAC (befee42078624a3b036869cf2a4a0c14)] -> class name: numfound]>
Alternatively, I've tried to select by CSS and got this:
Unable to locate element: {"method":"css selector","selector":"#resultsnum span.numfound"}
Perhaps my selector for CSS was wrong? What would be the most intuitive way to select this element using numfound?
RESOLVED: I was silly and didn't use .getText() for what I wanted.
This span is a WebElement. There are certain things that you can do with WebElement. Some of those are :
1. click on it. (Provided that element must be clickable)
2. getText() : Text between the <span> and </span> tag.
3. getSize();
4. getLocation();
5. getScreenShotAs(OUTPUT.Type)
6. getRect();
7. SendKeys(charSequence) (Provided that it can take input something).
and many more.
As of now, in your problem, you can get the text between span tag.
by using this code :
String spanText = driver.findElement(by.cssSelector("span[class="numfound"]")).getText();
and do String operations on it.
Let me know if you have any concerns about this.
You can use the By-selector only for elements inside of the tag.
To get the text of an element
you can use
driver.findElement(By.xpath("//span[#class='numfound']")).getText();
or (if you like more):
driver.findElement(By.className("numfound")).getText();
or get it from the page source by
String source = driver.getPageSource();
and extract a string from this, starting with "numfound" and ending with following tag
Then extract your string from this line.
You just have to do:
WebElement element = browser.findElement(By.className("numfound"));
//do whatever you want with your element, get attributes, etc.
For reference: https://www.seleniumhq.org/docs/03_webdriver.jsp#by-class-name

How can I get the inner text of an element in Selenium?

I'm working with a DOM node:
<input
type="form-control"
type="text"
data-bind="textInput: EnterpriseId"
disabled
autocomplete="off">
How can I get its value? I'm struggling since element.getText() does not work and returns a blank.
Try this:
WebElement element = driver.findElement(By.id("id value"));
String val = element.getAttribute("innerText")
I presume the element in question is an <input> element, so you may be able to use the element.getAttribute(String attribute) method like so:
String value = element.getAttribute("value");
This input tag is disabled, hence element.getText() returns a blank value.
Use element.getAttribute("textContent") instead.
You may be looking for the placeholder of an input text, because you might try:
element.getAttribute("placeholder");
You can go to your browser → open developer tools → inspect element you want to take attribute from → click Properties → check if that value is in InnerText.
Then do as it is mentioned in previous comments:
element_locator.get_attribute('InnerText')
I had the exact same issue! This post solved it for me:
How can I get the current contents of an element in webdriver
I used:
element = driver.find_elements_by_xpath(
'//button[#class="size-grid-dropdown size-grid-button"]')
element.text
As other's suggested, HTML's input nodes don't have a text attribute because they can store data in multiple formats in a value attribute.
This can be easily seen in the HTML input API specification where this form control can be of type radio, date, file upload and many more.
So, in your specific case, I'd suggest you check the webdriver's API for a method that's able to retrieve the value attribute.
As a bonus to evaluate innerText of an element within Selenium:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("yourEl")));
wait.until(ExpectedConditions.attributeToBe(By.id("yourEl"), "innerText", yourValue));
Documentation: attributeToBe
It works definitely, as I've tested it several times:
<input type="form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">
In your example, you don’t have any innerText. So you can only get attributes as mentioned before with the existing attributes. In your case:
type, data-bind, EnterpriseId and autocomplete. No value will be as this attribute isn’t created.
If you want to get only existing, this should be fine:
String example = driver.findElement(ByLocator(("")).getAttribute("any attribute of your input");
System.out.println(example);

how to click two identical attributes with Selenium WebDriver?

In firepath I saw two identical attributes, firepath has two results.
Here is the highlighted HTML code below in firebug:
<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">
And below is the whole code:
<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">
<span class="sr-only">Search</span>
</button>
NOTE: There is only 1 search button, I search it every where and there is only 1 but it shows two??
How to code this in selenium web driver?
The snippet from firepath:
Update:
Html code image, from firepath:
You can use XPath functions, for example:
position() returns the position of element at DOM
//button[#id='hdr_problem_task']/th[2]/button[position()=1]
last()
//button[#id='hdr_problem_task']/th[2]/button[last()]
something like first() doesn't exist, instead of this you can use index:
//button[#id='hdr_problem_task']/th[2]/button[1]
Also if button has some text you can use it as well:
//button[#id='hdr_problem_task']/th[2]/button[text()='button name']
or with contains()
//button[#id='hdr_problem_task']/th[2]/button[contains(text(), 'button name')]
UPDATE:
The button has name Search you can use XPath with - contains().
One more small suggestion, don't forget about future support. And instead of the following locator:
//*[#id='hdr_problem_task']/th[2]/button
Much better will be:
//button[#id='hdr_problem_task']/th[2]/button
You can use th tag's name attribute value in order to recognize the correct Search button, as shown below:
//th[#name='search'][1]/button/span[text()='Search']
Let me know, whether it works for you.

How to find input field is readonly or not in using selenium webdriver with java

I am using selenium webdriver with java to write the script. However, we have few fields which are getting disabled after click on button. We need to find this field are getting readonly mode or not. I have use isEnabled and isDisplayed but it is not working, the system displays NoSuchElementFound expection. Is there any way to handle this?
You can achieve this without having to execute JS, all you need to do is to get the DOM Attribute of the element which is "ReadOnly" this can be achieved using:
WebElement readOnly = driver.findElementBy(locator);
Assert.assertTrue(readOnly.getAttribute("readOnly").equals("true"),"Element ReadOnly")
Hope this helps....
You can do the following steps.
1. Use the WebDriverWait wait statement to make sure that the element is indeed present before you are doing the check.
2. Go through the HTML script and check what attribute is causing the HTML element to be read only. In the application that I use the attribute is'disabled'. When the value of the attribute 'disabled' was TRUE the element was disabled. For you it might be a different attribute. Then fetch the value of this attribute and you will find out if the element is enabled or disabled.
System.out.println(driver.findElement(By.xpath(txt_Username)).getAttribute("disabled"));
Using javascript, you can get this attribute value like -
var isReadOnly = document.getElementById("field").readOnly;
alert(isReadOnly); //displays true OR false
Please check browser compatibility for this attribute. :-)
<div class="col-sm-8 col-md-9 currency">
<input id="test" type="<some type>" value="777.00" class="read-only" readonly="true">
<input id="some_input_id_here" type="text" value="<some string value here>" name="<some name here>" class="read-only" readonly="true">
</div>
Lets use the above as example, which i have a form and i'm checking the elements using Chrome's DevTools (or any other preferred tool). Say i've got the css selector for the disabled fields that contains '777.00' in value and the element type 'css selector' is: #sampleForm > fieldset > div:nth-child(1) > div.col-sm-5.col-md-6 > input
Then my code to check on this element being read-only is:
//i have stored my elements somehwere on a file as String
String disElement = "#sampleForm > fieldset > div:nth-child(1) > div.col-sm-5.col-md-6 > input"
assertTrue(WebDriver.findElement(By.cssSelector(disElement)).getAttribute("class").equalsIgnoreCase("read-only"));
Note that i'm accessing the element's class layer and verify that it is "read-only" using TestNG assertTrue after getting the element's attribute of "class". The asserTrue boolean will return false if the element is not read only. Also check the layers of element you're accessing whether or not it is the parent, child, span, etc. Hope this can be of any help as well. thanks.
This code is working with my application:
public boolean runScript(String str){
String script ="if($("+'"'+"#City"+'"'+").attr("+'"'+str+'"'+")){return true}else{return false}";
System.out.println(script);
JavascriptExecutor js = (JavascriptExecutor) driver;
return (Boolean) js.executeScript(script);
}
int k=0;
do{
if(!runScript("HouseNo")){
System.out.println("Field is in readonly mode");
break;
}
Thread.sleep(1000);
}while(k<60);

Categories