2

In Java, how do I click a button with class, without ID, XPath or Class name?

1
  • Can you clarify what you mean by 'click a button with class, without... Class name?' Commented Mar 24, 2016 at 15:02

4 Answers 4

2

You can click by the compose text with xpath

driver.findElement(By.xpath("//div[contains(text(), 'COMPOSE')]")).click();

Or with cssSelector

driver.findElement(By.cssSelector("div:contains('COMPOSE')")).click();
Sign up to request clarification or add additional context in comments.

2 Comments

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector div:contains('COMPOSE') is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: An invalid or illegal selector was specified Command duration or timeout: 11.21 seconds
@Ram4Automation I fixed it in the answer, forgot the //.
1

you can use this instruction

driver.findElement(By.cssSelector("input[type='button']")).click();

6 Comments

It's not work out:(, I need to click a compose button in GMAIL after logged in.
can you extract the html code of the button so we can define a working selector
<div gh="cm" style="-moz-user-select: none;" class="T-I J-J5-Ji T-I-KE L3" role="button" tabindex="0">COMPOSE</div>
maybe you could try driver.findElement(By.cssSelector("div[role='button']")).click();
Yes it's clicking, but not a compose button, its clicking the search button
|
0

As an XPath:

//div[@class='T-I J-J5-Ji T-I-KE L3']

Comments

0
WebElement composeBtn = driver.findElement(By.xpath("//*[@class='T-I J-J5-Ji T-I-KE L3']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", composeBtn);

1 Comment

While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run.

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.