0

Unable to locate the element using id/name/xpath/CSSSelector

Tried the below codes and both failed to give response

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'form\']/p/button/span")));
driver.findElement(By.xpath("//*[@id=\'form\']/p/button/span")).click();

and

WebElement checkout = driver.findElement(By.xpath("//[@id=\'form\']/p/button/span"));
checkout.click();

HTML

 <button type="submit" name="processCarrier" class="button btn btn-default standard-checkout button-medium" style="">
    <span> Proceed to checkout <i class="icon-chevron-right right"></i> </span>
 </button>
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    HTML: – user1677627 Apr 16 '19 at 13:15
  • Maybe that element is inside an iframe and webdriver isn't inside that iframe. Look for any iframes and if the element is inside that, then do `driver.switchTo().frame("frame_id_or_index_goes_here")` – Jayesh Doolani Apr 16 '19 at 14:21
  • Does any answer resolve you issue ? If yes then please accept the answer by click on tick mark below the vote count on answer. So it can be helpful for others. If no then update your question with more details or feel free to ask in comments. Thanks :) – NarendraR Mar 05 '20 at 12:14

3 Answers3

0

Try Following CSS Selector.

WebElement checkout = driver.findElement(By.cssSelector("button.standard-checkout span"));
checkout .click();

Or yon can use WebDriverWait and Css Selector.

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout span")));
element.click()
KunduK
  • 26,790
  • 2
  • 10
  • 32
0

Probably you are getting org.openqa.selenium.InvalidSelectorException because you should use * after // to match any node(tag) who has id=form or the specific tag name.

change it to //*[@id='form']/p/button/span

Or use more specific like

xpath : //button[@name='processCarrier'] equivalent CSS : button[name='processCarrier']

And use implicit/explicit wait to make element available in DOM to perform actions.

NarendraR
  • 6,770
  • 7
  • 35
  • 69
0

Presumably you will invoke click() on the <button> element, so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout.button-medium[name='processCarrier']>span"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='button btn btn-default standard-checkout button-medium' and @name='processCarrier']/span"))).click();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217