9

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.

2 Answers 2

15

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

@FindBy(how=How.XPATH, using="//button[text()='Sign in']")
WebElement signInButton;
Sign up to request clarification or add additional context in comments.

1 Comment

Note that if the text is not a direct child of the button it won't match, e.g. <button><span>Sign in</span></button> won't match. Instead you can use the xpath //button[contains(., 'Sign in')]
-2

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.