How to use Regular expression to find a web element.

I want to fetch out the text of all the elements whose id start with “WebPart_twp”

The Complete xpath is given Below:
.//*[@id=’WebPart_twp1174308228′]/tbody/tr/td/div

I have Tried this with below Example but unfortunately it doesn’t work
driver.findElement(By.xpath(“.//*[@id=’WebPart_twp([0-9]+)’]/tbody/tr/td/div”));

Error Message:
Unable to locate element: {“method”:”xpath”,”selector”:”.//*[@id=’WebPart_twp([0-9]+)’]/tbody/tr/td/div”

Please do some needful.

Thanks,
Vandana Maurya

Default Asked on December 15, 2014 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)
    Best answer

    Hi Vandana,

    Try this :

    
    List <WebElement> elementList = driver.findElements(By.xpath(".//*[contains(@id,"WebPart_twp")]"));
    
    

    Regards,
    Lakshay Sharma

    Professor Answered on December 15, 2014.

    Hi
    and to get Text  use
    For(WebElment text: elementList)
    {
    String ElmentText=text.getText():
    system.out.println(Elment);

    }

    on December 15, 2014.

    Thank You so much Lakshay  and Ankit 🙂

    on December 16, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.