0

I'm trying to get XPATH for an error message on github login page.(https://github.com/login). But I can't get XPATH for this element in chrome

I put incorrect credential and saw 'Incorrect username or password' After that:

  1. I tried to copy XPATH by chrome $x("//*[@id='js-flash-container']/div/div/text()") but it returned (2) [text, text]
  2. $x("//div[contains(text(), 'Incorrect username or password.')]") but it returned nothing

HTML:

<div class="flash flash-full flash-error">
  <div class="container">
    <button class="flash-close js-flash-close" type="button" aria-label="Dismiss this message">
      <svg class="octicon octicon-x" viewBox="0 0 12 16" version="1.1" width="12" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48L7.48 8z"></path></svg>
    </button>

      Incorrect username or password.

  </div>
</div>

I need an error message. But I haven't

private By error = By.xpath("//*[@id='js-flash-container']/div/div/text()");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Iryna
  • 1

3 Answers3

2

It is much easier to use cssSelector...

String error = driver.findElement(By.cssSelector("div .flash")).getText().trim();
System.out.println(error);

But if you want XPATH use:

String error = driver.findElement(By.xpath('//div[@class="container"]')).getText().trim();
System.out.println(error);
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
1

To extract the error message Incorrect username or password. on github login page https://github.com/login you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • cssSelector:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#js-flash-container div.container"))).getText());
    
  • xpath:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='js-flash-container']//div[@class='container']"))).getText());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Since the ID is unique on the page, you can remove the `div` from the first part locators, e.g. CSS selector would be `#js-flash-container div.container`. – JeffC Aug 12 '19 at 20:26
  • Actually... since there's a ton of whitespace before and after the desired text even when specifying the `div.container`, you might as well just use the ID alone, `js-flash-container`, and `.trim()`... since you're going to have to trim anyway. – JeffC Aug 12 '19 at 20:27
0

In firefox I've located that element and found the xpath to be:

/html/body/div[3]/main/div/form/div[2]/div/div

I loaded up firefox selenium and found this element by xpath:

WebElement error_msg = driver.findElement(By.xpath( ELEMENT_XPATH ));

Then to get text I use:

Text error_msg_str = error_msg.getText();

You could then compare this string with "Incorrect username or password." , and if these match you raise an error.

johnabro
  • 81
  • 2
  • 9