【问题标题】:How to click on a anchor Tag using selenium webDriver如何使用 selenium webDriver 单击锚标记
【发布时间】:2020-01-17 11:54:54
【问题描述】:

我无法点击有一些可见性问题的按钮。我需要先将鼠标悬停在此处以获取链接,然后我需要单击该链接。

<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>

代码试验:

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 webdriverwait


【解决方案1】:

所需的元素是一个动态元素,因此对于click(),您必须为elementToBeClickable() 诱导WebDriverWait 元素,您可以使用以下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();
    

【讨论】:

    【解决方案2】:

    你可以尝试用webdriver wait点击element to receive click

    By buttonBy = By.cssSelector("a.cardPreviewLink.expand-icon > button"));
    
    WebDriverWait wait = new WebDriverWait(driver, 50);
    wait.until(ExpectedConditions.elementToBeClickable(buttonBy);
    

    如果上述方法不起作用,您可以尝试使用click using JS。在这里,我只是在等待visibility of element,因为如果元素可以接收点击,那么第一种方法应该可以工作。

    wait.until(ExpectedConditions.visibilityOfElementLocated(buttonBy);
    
    WebElement button=driver.findElement(buttonBy);
    
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    
    executor.executeScript("arguments[0].click();", button);
    

    【讨论】:

    • 在 wait.until 行抛出错误。那么如何使用js点击呢?
    • 如果你要移除等待它应该点击,因为 JS 不依赖于元素的状态
    【解决方案3】:

    By.className() 不适用于包含空格的名称 - cardPreviewLink expand-icon。而是尝试使用 cssSelector 或 xpath。

    Xpath 示例:

    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' 应该可以工作。如果没有,如 Debanjan 所述,请尝试使用“elementToBeClickable”。
    此外,wait.until 本身将返回 WebElement 对象。你可以用它来点击它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2020-11-10
      • 2017-04-06
      • 1970-01-01
      • 2020-08-22
      相关资源
      最近更新 更多