Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium css selector for button text

HTML looks like below :

<button class="btn btn-primary pull-right" type="submit">Sign in</button>

There are multiple buttons with same class and type value. However I need to click only Sign in button using CSS selector. I tried the following but none of them is working

@FindBy(how=How.CSS, using="button:contains('Sign in')") WebElement signInButton;

@FindBy(how=How.CSS, using=".btn btn-primary pull-right[text='Sign in']") WebElement signInButton;

Also point me to some complex CSS selector examples site.

like image 446
Saagar Avatar asked May 18 '26 23:05

Saagar


1 Answers

A CSS selector doesn't support text evaluation. Use an XPath instead:

@FindBy(how=How.XPATH, using="//button[text()='Sign in']")
WebElement signInButton;
like image 85
michael Avatar answered May 20 '26 13:05

michael