Saagar
Saagar

Reputation: 834

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.

Upvotes: 8

Views: 27666

Answers (2)

michael
michael

Reputation: 949

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

@FindBy(how=How.XPATH, using="//button[text()='Sign in']")
WebElement signInButton;

Upvotes: 14

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Hi plz do it like below considering that your sign in button is first in the list (if not plz change as per your requirement)

driver.findElements(By.cssSelector(".btn.btn-primary.pull-right")).get(0).click();

Upvotes: -1

Related Questions