Veirfy Element not has attribute

Hello guys,

I am trying to verify if an web element has some attribute or not. I have a drop down menu, which is dynamic, so data can be different any time. For that I’ve captured a class, for example:

List<WebElements> dropDown_elements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-gZMcBi sc-dVhcbM cCJOQh']"))

And then I used this:

for(int i = 0; i < dropDown_elements.size(); i++) {
	WebUI.verifyElementNotHasAttribute(dropDown_elements.get(i), 'disabled', 5)
}

Well from that, I expected that, dropDown_elements.get(i) would work, but it does not work.
Can I somehow use different method ?

I have 5 elements, those 5 elements are in the drop down menu. These elements with disabled attribute can not be clicked. Well, I want to write a logic: if element has not disabled as an attribute, then click that, else print that this object is disabled. But, I can’t check the attribute value by index.

def static getDropDownElements() {
		WebUI.click(findTestObject('Object Repository/ClickDropDownForm'))
		List<WebElements> dropDown_elements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-gZMcBi sc-dVhcbM cCJOQh']"))
		for(int i = 0; i < dropDown_elements.size(); i++) {
			if(...........) {
				dropDown_elements.get(i).click()   //select object in the drop down menu
			}
			else {
				KeywordUtil.logInfo("...")
			}
			WebUI.delay(2)
			WebUI.click(findTestObject('Object Repository/ClickDropDownForm'))  // click object to open drop down menu
			dropDown_elements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-gZMcBi sc-dVhcbM cCJOQh']"))
		}
	}

I can’t help you with all that WebUi/WebElements stuff using XPath, but I can point you in the right direction using JavaScript and css selectors.

Using this page as the test bed (I mean THIS PAGE, i.e. this thread) …

You can try these in your browser console:

document.querySelector("#main>div").hasAttribute("disabled") //returns false

document.querySelector("#main>div").hasAttribute("id") // returns true

document.querySelector("#main>div").getAttribute("id") // returns "ember699"

I suggest you write a groovy Keyword hasAttr() that wraps the JavaScript and returns true/false. You can then use hasAttr in your if statement.

if(hasAttr(element, "disabled")) { ...

You could try using the list function each or eachWithIndex and specify your attribute checks within the closure.

http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/List.html#eachWithIndex(groovy.lang.Closure)

Hello @Russ_Thomas, I have made some things with querySelector, but I did not understand how to use it in Katalon studio.

I’ve found this, but I don’t know what to type as the second parameter:

def k = WebUI.executeJavaScript('document.querySelectorAll(\'.bbPtHq\')[3].hasAttribute(\'disabled\')', null)

Also, can I use something like that ?
for(int i = 0; i < document.querySelectorAll('.bbPtHq').size(); i++) { document.querySelectorAll('.bbPtHq')[i].hasAttribute('disabled') }

querySelector and querySelectorAll are DOM APIs used in JavaScript. Each needs a CSS selector, not an XPath.

In groovy? No. (and again, you’re passing an XPath to querySelector - it should be a CSS selector: .bbPtHq.

Here’s what you need to do. Start by using the browser console, like I said. Build a working CSS selector that returns a single element that you want to click. That selector can then be modified to be used in a groovy for loop statement.

My intention is not to write the code for you. (It would be difficult anyway, you have not posted the HTML you’re trying to work with).

What I will do is help you _work through the code.

2 Likes