简体   繁体   English

无法单击 selenium webdriver 中的“按钮”(使用 Java)

[英]Unable to click the 'button' in selenium webdriver (using Java)

This is the code where I want to locate the href button :这是我想要找到 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>

I am using the following xpath but it is not working :我正在使用以下 xpath,但它不起作用:

driver.findElement(By.xpath("/html/body/div/div[2]/form/div[4]/div/button")).click();

this the image这是图像

As per the HTML you have provided, the href attribute is within an <a> tag.根据您提供的HTMLhref属性位于<a>标记内。 So we will construct a unique logical xpath to locate the WebElement and invoke click() method as follows :因此,我们将构建一个唯一的逻辑xpath来定位WebElement并调用click()方法,如下所示:

driver.findElement(By.xpath("//a[@class='btn grey-edit' and @data-original-title='Login']")).click();

OR或者

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

You can also use the JavaScriptExecutor for the same你也可以使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM