In Java, how do I click a button with class, without ID, XPath or Class name?
4 Answers
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();
2 Comments
Ram4Automation
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
Guy
@Ram4Automation I fixed it in the answer, forgot the
//.you can use this instruction
driver.findElement(By.cssSelector("input[type='button']")).click();
6 Comments
Ram4Automation
It's not work out:(, I need to click a compose button in GMAIL after logged in.
Olivier Boissé
can you extract the html code of the button so we can define a working selector
Ram4Automation
<div gh="cm" style="-moz-user-select: none;" class="T-I J-J5-Ji T-I-KE L3" role="button" tabindex="0">COMPOSE</div>
Olivier Boissé
maybe you could try driver.findElement(By.cssSelector("div[role='button']")).click();
Ram4Automation
Yes it's clicking, but not a compose button, its clicking the search button
|
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
Benjamin W.
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.