1

I like to test a complex webapplication written in GWT, and I have some question about how Selenium works.

I have two calendars (monthCalendar1 and monthcalender2). Both have a label with the month and year

image of calendars

I can reach the month and year label in the second calendar with next statement :

WebElement webelement = driver.findElement(By.xpath("//div[contains(@class,'monthCalendar2')]//div[contains(@class,'monthAndYearLabel')]"));
    String s = webelement.getText();
    //s = December 2018

But I would like to split the website in logical parts, so I want to use :

  WebElement webelement = driver.findElement(By.xpath("//div[contains(@class,'monthCalendar2')]"));
        WebElement webelement2 = webelement.findElement(By.xpath("//div[contains(@class,'monthAndYearLabel')]"));
    webelement2.getText();
    String s = webelement2.getText();
    //s = November 2018

It looks like webelement2 starts searching from the start of the website, and not from webelement. Can I use the second approach? How?

Kind regards Wim

Community
  • 1
  • 1

1 Answers1

1

You need to add . before the first // to tell the xpath to use current context

webelement.findElement(By.xpath(".//div[contains(@class,'monthAndYearLabel')]"));
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Mmm, that doesn't work. I get : org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//div[contains(@class,'monthAndYearLabel')]"} – Wim Willemsens Nov 08 '18 at 14:44
  • @WimWillemsens I can't help much without the html, but this is the way. Maybe try adding some wait https://stackoverflow.com/a/11738528/5168011 – Guy Nov 08 '18 at 14:48
  • Hi, I'm sorry it looks that this is working well. Some copy paste issue. Thanhx! – Wim Willemsens Nov 08 '18 at 14:50