如何使用 selenium webDriver 单击锚标记
How to click on a anchor Tag using selenium webDriver
我无法单击存在某些可见性问题的按钮。我需要先将鼠标悬停在此处以获取 link,然后我需要单击它。
<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&type===&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 的元素,您可以使用以下任一方法以下 :
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() 不适用于包含空格的名称 - 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 本身就是 return WebElement 对象。您可以使用相同的方式点击它。
您可以尝试用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);
我无法单击存在某些可见性问题的按钮。我需要先将鼠标悬停在此处以获取 link,然后我需要单击它。
<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&type===&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 的元素,您可以使用以下任一方法以下
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() 不适用于包含空格的名称 - 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 本身就是 return WebElement 对象。您可以使用相同的方式点击它。
您可以尝试用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);