| || |
- 1 Post By dlai
Regular Expression in Selenium Web Driver
Can anyone please tell me how to use the regular Expression here. I need to click the logout Button here. The title of the Logout button starts with the UserID. I need a regular expression.
I am using below code
WebElement webElement =driver.findElement(By.xpath("//img[contains(@title='*.logout from App)]"));
Actions userAction = new Actions(driver);
Actions dblClick = userAction.doubleClick(webElement);
But it is not working
<img class="displayed" border="0" title="Shruti Bhatia: logout from App" alt="" src="../images/../plugins/LookAndFeel/style/images/sm_right_circle.png">
I need to click the Logour Button
First off, xpath contains does not support regular expressions. Contains is merely just a strict substring. There is a 'matchs' xpath function, however not all browsers will support that yet.
Originally Posted by shrutibhatia1980
2nd, contains should be called like this, contains(@title, 'logout from App').
3rd, usually it's better to add a locator than to bend over backwards to try to locate an element. Title is a pretty brittle property. It's better to just edit the template and add something like data-automation='logout', to the element, then you can simply locate it by By.css("[data-automatin='logout']"), and that won't break if the designer chooses to relabel the button.