[英]How to click on a anchor Tag using selenium webDriver
我無法點擊有一些可見性問題的按鈕。 我需要先將鼠標懸停在此以獲取鏈接,然后我需要單擊相同的鏈接。
<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 本身將返回 WebElement 對象。 您可以使用它來單擊它。
您可以嘗試使用webdriver wait
單擊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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.