javascript 量角器按名称标签获取元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32989447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:59:11  来源:igfitidea点击:

protractor get element by name tag

javascriptseleniumtestingselenium-webdriverprotractor

提问by Adrian Melzer

Currently I am working with protractor and Selenium web Driver.

目前我正在使用量角器和 Selenium Web 驱动程序。

I have the following problem:

我有以下问题:

I have a html page, and I make protractor clicking a button. Then a window pops up. This window contains a text box with the Name "Description":

我有一个 html 页面,我让量角器点击了一个按钮。然后弹出一个窗口。该窗口包含一个名称为“描述”的文本框:

<input type="Text" name="Description" ... />

Now when I try the following:

现在,当我尝试以下操作时:

element(by.css('[name="Description"]')).sendKeys("rabbababab");

The browser does nothing, but protractor does not throw an error. No text is typed into the TextBox. Unfortunatelly, the name is the only way to identfy the input-TextBox.

浏览器什么都不做,但量角器不会抛出错误。没有文本输入到文本框中。不幸的是,名称是识别输入文本框的唯一方法。

What am I doing wrong?

我究竟做错了什么?

回答by mooses

Selecting directly by name works as well:

直接按名称选择也有效:

element(by.name('Description')).sendKeys("rabbababab");

回答by Adrian Melzer

OK guys, ive found the issue.

好的,伙计们,我找到了问题所在。

It wasnt an alert, its just a div, and all other controls are locked for user Input. but the Dialog covers a TextBox, wich has the same css-properties. So protractor just writes into the covered TextBox and i couldnt see it...

它不是警报,它只是一个 div,并且所有其他控件都被锁定以供用户输入。但是对话框覆盖了一个文本框,它具有相同的 css 属性。所以量角器只是写入覆盖的文本框,我看不到它......

The Problem is solved

问题已经解决了

回答by yojna

Sometimes if that element is inside iframe then you have to switch to that iframe. Just check that is there any iframe or modal available?

有时,如果该元素在 iframe 内,则您必须切换到该 iframe。只需检查是否有任何 iframe 或模态可用?

otherwise your code seems correct.

否则你的代码似乎是正确的。

回答by Girish Sortur

There is an inbuilt prompt handler in protractor where you can identify it and then send the data into the input field that you want. Here's how -

量角器中有一个内置的提示处理程序,您可以在其中识别它,然后将数据发送到您想要的输入字段中。就是这样 -

browser.Alert.sendKeys("rabbababab");

Note: The pop up window should have an input that it can accept some data into it else you command will fail.

注意:弹出窗口应该有一个输入,它可以接受一些数据,否则你的命令将失败。

If the above solution doesn't work then try sending data by switching to the pop-up and then sending text to it. Here's how -

如果上述解决方案不起作用,请尝试通过切换到弹出窗口然后向其发送文本来发送数据。就是这样 -

browser.driver.switchTo().alert().sendKeys('rabbababab');

If at all there are many prompts, then you can use window handles function to switch to the one that you want. Here's how -

如果有很多提示,那么您可以使用窗口句柄功能切换到您想要的那个。就是这样 -

browser.getAllWindowHandles().then(function(handles){
    browser.switchTo().window(handles[1]).then(function(){ //change the array index based on your pop-up's count
        element(by.css('[name="Description"]')).sendKeys("rabbababab");
    });
});

Hope it helps.

希望能帮助到你。

回答by xrayder

Maybe you have multiple objects which have name="Description" in your application. You can find this in Chrome:

也许您的应用程序中有多个具有 name="Description" 的对象。你可以在 Chrome 中找到这个:

  1. Right-click on the object
  2. Click on Inspect element
  3. Press CTRL+F
  4. Type [name="Description"] and see how many results it finds.

    element(by.css('[name="Description"]'))

  1. 右键单击对象
  2. 单击检查元素
  3. 按 CTRL+F
  4. 输入 [name="Description"] 并查看它找到了多少结果。

    元素(by.css('[name="Description"]'))

is the same as

是相同的

$('[name="Description"]')

If you find more than one, then you can try the following: 1. Try a click on the field before sending keys to it

如果您找到多个,那么您可以尝试以下操作: 1. 在向其发送密钥之前尝试单击该字段

2.

2.

// This will search for the element of input with the name="Description" attribute
$('input[name="Description"]').sendKeys('rabbababab');

3. You can try to put the following line, before sending keys to it : browser.waitForAngular(); // wait until the angular app loads

3.您可以尝试在向其发送密钥之前放置以下行:browser.waitForAngular(); // 等待 Angular 应用程序加载

Let us know how it worked.

让我们知道它是如何工作的。

回答by Ramya Raju

Try using this, $('input [name=Description]').sendKeys("rabbababab"); or element(by.css('input [name=Description]')).sendKeys("rabbababab");

尝试使用这个, $('input [name=Description]').sendKeys("rabbababab"); 或 element(by.css('input [name=Description]')).sendKeys("rabbababab");