[英]Unable to click on a button present in iframe using selenium webdriver and java
[英]Unable to click the 'button' in selenium webdriver (using Java)
這是我想要找到 href 按鈕的代碼:
<a class="btn grey-edit" data-original-title="Login" data-placement="top"
data-toggle="tooltip" href="/users/userlogin/3">
<i class="fa fa-sign-in" aria-hidden="true"></i>
我正在使用以下 xpath,但它不起作用:
driver.findElement(By.xpath("/html/body/div/div[2]/form/div[4]/div/button")).click();
根據您提供的HTML
, href
屬性位於<a>
標記內。 因此,我們將構建一個唯一的邏輯xpath
來定位WebElement
並調用click()
方法,如下所示:
driver.findElement(By.xpath("//a[@class='btn grey-edit' and @data-original-title='Login']")).click();
或者
driver.findElement(By.xpath("//a[@class='btn grey-edit']/i[@class='fa fa-sign-in']")).click();
WebElement btnElem= driver.findElement(By.xpath("//a[@class='btn grey-edit' and @href='/users/userlogin/3']"));
btnElem.click();
你也可以使用 JavaScriptExecutor 來做同樣的事情
WebElement btnElem= driver.findElement(By.xpath("//a[@class='btn grey-edit' and @href='/users/userlogin/3']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", btnElem);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.