简体   繁体   中英

How to click on a anchor Tag using selenium webDriver

I am not able to click on the button having Some visibility issues. I need to hover around this first to get the link then i need to click on the same.

<a tabindex="0" 
   class="cardPreviewLink expand-icon" 
   aria-label="card opens in new tab" 
   target="_blank" 
   id="card-preview-link-19479" 
   href="/card/19479?$filters@$pattern=10532372&amp;type===&amp;dimension=chargeback_id"> 
  <button class="MuiButtonBase-root MuiIconButton-root" tabindex="-1" type="button">
    <span class="MuiIconButton-label">
      <svg class="MuiSvgIcon-root open-icon" 
           focusable="false" 
           viewBox="0 0 24 24" 
           aria-hidden="true" 
           role="presentation">
        <path d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z"/>
      </svg>
    </span>
  </button>
</a>

Code trials:

WebDriverWait wait4 = new WebDriverWait(driver, 60);
wait4.until(ExpectedConditions.visibilityOfElementLocated(By.className("cardPreviewLink expand-icon")));
driver.findElement(By.className("cardPreviewLink expand-icon")).click();

Error:

Timeout Exception because of No such Element Exception

The desired element is a dynamic element so to click() on the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :

  • cssSelector :

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.cardPreviewLink.expand-icon > button.MuiButtonBase-root.MuiIconButton-root > span.MuiIconButton-label"))).click();
  • xpath :

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='cardPreviewLink expand-icon']/button[@class='MuiButtonBase-root MuiIconButton-root']/span[@class='MuiIconButton-label']"))).click();

By.className() won't work with names having spaces - cardPreviewLink expand-icon . Instead try to use cssSelector or xpath.

Xpath Example:

WebDriverWait wait4 = new WebDriverWait(driver, 60);
WebElement element = wait4.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@class,'cardPreviewLink'][contains(@class,'expand-icon']")));
element.click();

'visibilityOfElementLocated' should work. If it didn't, as mentioned by Debanjan, try with 'elementToBeClickable'.
Also, wait.until will itself return WebElement object. You can use the same to click on it.

You can try to click with webdriver wait for element to receive click .

By buttonBy = By.cssSelector("a.cardPreviewLink.expand-icon > button"));

WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until(ExpectedConditions.elementToBeClickable(buttonBy);

If above approach will not work you can try with click using JS . Here I am just waiting for visibility of element as If element can receive click then First approach should work.

wait.until(ExpectedConditions.visibilityOfElementLocated(buttonBy);

WebElement button=driver.findElement(buttonBy);

JavascriptExecutor executor = (JavascriptExecutor)driver;

executor.executeScript("arguments[0].click();", button);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM