0

I've spent a couple of hours trying to click on a button using various selectors before asking a question here, but nothing seems to work

WebElement add= driver.findElement(By.partialLinkText("+"));
WebElement add= driver.findElement(By.xpath("/*[name()='svg']/*[name()='button']"));
WebElement confirm = driver.findElement(By.xpath("//a[contains(@class, 'IconButton-h85035-0 indexstyles__PlusButton')]"));

No available examples have a similar html layout as the page I'm working with. I am not sure how to create the xPath to the button. I would appreciate any hints on how to create xPath, or the CSS selector, no ready solution is expected, but any help to understand how to refere to that particular element.

That is the code of the plus button:

<button data-testid="tselectionSpinbuttonPlus" type="button" tabindex="-1" aria-hidden="true" width="44px" height="44px" class="IconButton-h85035-0 indexstyles__PlusButton-sc-108enfc-3 bAZDfp">
    <svg viewBox="0 0 24 24" width="1.5em" height="1.5em" aria-hidden="true" focusable="false" class="BaseSvg-sc-9y47q5-0 PlusIcon___StyledBaseSvg-sc-11rza9m-0 VCaQT">
      <path d="M13 11V3h-2v8H3v2h8v8h2v-8h8v-2h-8z">
      </path>
    </svg>
 </button>

enter image description here

kryzystof
  • 190
  • 2
  • 13
  • 1
    try this `driver.findElement(By.cssSelector("button[data-testid='tselectionSpinbuttonPlus']")).click()` – NarendraR Mar 05 '20 at 09:41

2 Answers2

1

To locate/click on the element you can use either of the following Locator Strategies:

  • cssSelector:

    driver.findElement(By.cssSelector("button[data-testid='tselectionSpinbuttonPlus']")).click();
    
  • xpath:

    driver.findElement(By.xpath("//button[@data-testid='tselectionSpinbuttonPlus']")).click();
    

Ideally to locate/click on the element you need to induce WebDriverWait for the 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[data-testid='tselectionSpinbuttonPlus']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@data-testid='tselectionSpinbuttonPlus']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Pls try with below: Code snipt

WebElement add= driver.findElement(By.partialLinkText("+"));
        Actions ac1 = new Actions(driver);
                    ac.clickAndHold(add).build().perform();