如何使用Selenium webDriver单击锚标记

问题描述 投票:0回答:1

我无法单击出现一些可见性问题的按钮。我需要先将鼠标悬停在该位置上才能获得链接,然后单击相同的按钮。

<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"></path></svg></span></button></a>

代码试用:

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

错误:

Timeout Exception because of No such Element Exception
java selenium xpath css-selectors webdriverwait
1个回答
0
投票

所需元素是动态元素,因此单击文本为Production的元素,可以使用以下解决方案之一:

  • 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();
    
© www.soinside.com 2019 - 2024. All rights reserved.