In our application almost in every screen we have a button with text 'New', Here is the html source for one of the button:
<button id="defaultOverviewTable:j_id54" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover" type="submit" name="defaultOverviewTable:j_id54" role="button" aria-disabled="false">
<span class="ui-button-text ui-c">New</span>
</button>
I have tried using the below statement to click on the button:
driver.findElement(By.xpath("//button[[@type, 'submit'] and [text()='New']]")).click();
But this was not working
org.openqa.selenium.InvalidSelectorException: The given selector //button[[@type= 'submit'] and [text()='New']] is either invalid or does not result in a WebElement.
Currently I am using the below code to click on the button:
List<WebElement> allButt = driver.findElements(By.tagName("button"));
for (WebElement w : allButt)
{
if (w.getText().matches("New"))
{
w.click();
break;
}
}
As I have almost 150 buttons in the page. Is there any other way?
Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.
XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.
From the Elements panel in Chrome's Developer Tools, you can press Ctrl+F and enter an XPath to view the search results for an XPath selector.
text(): A built-in method in Selenium WebDriver that is used with XPath locator to locate an element based on its exact text value. contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on partial text match.
Your xpath syntax is wrong - you don't need the inner set of square brackets - but even if you fix this:
//button[@type, 'submit' and text()='New']
it won't select what you want it to. The problem is that the "New" is not text contained directly in the button element but is inside a child span element. If instead of text()
you just use .
then you can check the whole string value of the element (the concatenation of all descendant text nodes at whatever level)
//button[@type='submit' and contains(., 'New')]
Or check span
instead of text()
:
//button[@type='submit' and span='New']
(submit buttons containing a span whose value is "New")
Try this xpath instead:
//button[@type='submit']/span[.='New']
Demo
http://www.xpathtester.com/xpath/ff393b48183ee3f373d4ca5f539bedf2
EDIT
Following comment from @Ian Roberts, you can use the following xpath expression instead if the click on the button element is important:
//button[@type='submit']/span[.='New']/..
The very simple solution for the above issue is to use span with option contains(text(),'')
.
You can use the following xpath code
//span[contains(text(),'New')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With