简体   繁体   English

如何使用 selenium webdriver 单击角度元素?

[英]How to click an angular element with selenium webdriver?

Need help on below to click() i can't click on the Button 'Connexion'.在下面需要帮助才能点击()我无法点击按钮“连接”。 I can locate element in Chrome devtool but my program still fails.我可以在 Chrome devtool 中找到元素,但我的程序仍然失败。

Button HTML :按钮 HTML :

<button class="btn btn-lg hoverableButton ng-scope" translate="LOGIN" ng-disabled="authenticationCtrl.disableMageConnectButton()" ng-click="onSubmit()">Connexion</button>

I have tried with below code我试过下面的代码

By button = By.xpath("//button[@class='btn btn-lg hoverableButton ng-scope' and contains(@ng-click, 'authenticationCtrl.onSubmitMage()')]");

WebDriverWait wait = new WebDriverWait(webdriver, 15);
    wait.until(ExpectedConditions.elementToBeClickable(button));
    webdriver.findElement(button).click();

The error Message :错误信息:

Expected condition failed: waiting for element to be clickable: By.xpath: //button[@class='btn btn-lg hoverableButton ng-scope' and contains(@ng-click, 'authenticationCtrl.onSubmitMage()')] (tried for 15 second(s) with 500 milliseconds interval)预期条件失败:等待元素可点击:By.xpath: //button[@class='btn btn-lg hoverableButton ng-scope' and contains(@ng-click, 'authenticationCtrl.onSubmitMage()')] (以 500 毫秒的间隔尝试了 15 秒)

and

no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='btn btn-lg hoverableButton ng-scope' and contains(@ng-click, 'authenticationCtrl.onSubmitMage()')]"}没有这样的元素:无法定位元素:{"method":"xpath","selector":"//button[@class='btn btn-lg hoverableButton ng-scope' and contains(@ng-click, 'authenticationCtrl .onSubmitMage()')]"}

I tested the xpath on devtools and xpath and it found the button.我在 devtools 和 xpath 上测试了 xpath,它找到了按钮。

Thanks.谢谢。

If如果

webdriver.findElement(button).click();

throws投掷

no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='btn btn-lg hoverableButton ng-scope' and contains(@ng-click, 'authenticationCtrl.onSubmitMage()'没有这样的元素:无法定位元素:{"method":"xpath","selector":"//button[@class='btn btn-lg hoverableButton ng-scope' and contains(@ng-click, 'authenticationCtrl .onSubmitMage()'

It could be cause of element is in iframe or locator could not found in DOM.这可能是因为元素在 iframe 中或在 DOM 中找不到定位器。

Since you are saying that it is present in DOM, I would probably say, we may have an iframe issue here.既然你说它存在于 DOM 中,我可能会说,我们这里可能有一个 iframe 问题。

Iframe :框架:

The tag specifies an inline frame.该标签指定了一个内嵌框架。

An inline frame is used to embed another document within the current HTML document.内嵌框架用于在当前 HTML 文档中嵌入另一个文档。

In Selenium, we need to switch the driver focus to particular iframe in order to interact with the elements which are inside of the iframe :在 Selenium 中,我们需要将驱动程序焦点切换到特定的 iframe 以便与 iframe 内部的元素进行交互:

driver.switchTo.frame("Frame_ID");

and then you should be able to do :然后你应该能够做到:

webdriver.findElement(By.xpath("//button[text()='Connexion']")).click();

Update 1 :更新 1:

There are two button with same name, I am pretty much sure that xpath index will work有两个同名的按钮,我非常确定xpath索引会起作用

for first button :对于第一个按钮:

(//button[text()='Connexion'])[1]

second button :第二个按钮:

(//button[text()='Connexion'])[2]

Update 2 :更新2:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//button[text()='Connexion'])[2]"))).click();

单击按钮之前,请使用 isEnabled 检查按钮是否已启用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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