gpt4 book ai didi

java - 如何使用 selenium webDriver 单击 anchor 标记

转载 作者:行者123 更新时间:2023-12-02 09:06:24 25 4
gpt4 key购买 nike

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

<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

最佳答案

所需的元素是动态元素,因此要在该元素上click(),您必须为elementToBeClickable()引发WebDriverWait并且您可以使用以下任一 Locator Strategies :

  • css选择器:

    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();

关于java - 如何使用 selenium webDriver 单击 anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59786864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com