Chris Doan
Chris Doan

Reputation: 43

How to click a ng-click button with selenium java

I'm doing an automated web test for my job and I'm having trouble with my code. It didn't do anything Here's the code from the website I'm trying to work with:<button class="fa fa-2x sidebar-button pull-left fa-bars" ng-click="offcanvasleft()" ng-class="activeLeft ? 'fa-bars' : 'fa-bars'">::before</button>

And here is my code:driver.findElement(By.cssSelector("//*[@id=\"mainrow\"]/div/div/button[1]")).click();

Can someone help me with this? Thank you!

Upvotes: 4

Views: 3521

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193058

As per the HTML you have shared the WebElement is a Angular Element so you have to induce WebDriverWait with ExpectedConditions clause as elementToBeClickable as follows :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='fa fa-2x sidebar-button pull-left fa-bars' and starts-with(@ng-class,'activeLeft')]"))).click(); 

Upvotes: 0

iamsankalp89
iamsankalp89

Reputation: 4739

You can use like this:

WebELement btnBefore= driver.findElement(By.XPATH("//button[contains(text(),'::before')]");
btnBefore.click();

Upvotes: 1

Chris Brocious
Chris Brocious

Reputation: 161

driver.findElement(By.XPATH("//button[@class="fa fa-2x sidebar-button pull-left fa-bars]").click();

This is assuming java syntax is right. I write selenium in python mostly.

Upvotes: 0

Related Questions