Selenium - 如何从 div > 通过 id 输入发送输入?

Selenium - How to send input from div > input by id?

所以我遇到了一个问题,我试图在输入字段中输入文本,但我意识到有两个具有相同 ID 名称的相同 ID,我在这里寻求有关如何指定哪个 div -> 我想使用的输入。

到目前为止我所做的是:

it('Entering First Name', function (done) {

    browser.driver
        .then(() => browser.wait(EC.visibilityOf(element(by.xpath('//input[@id="pp-cc-first-name-field"]'))), 50000, "Timed out finding 'First Name' element"))
        .then(() => element(by.xpath('//input[@id="pp-cc-first-name-field"]')).sendKeys("hello world")
        .then(() => done());
});

使用这个 HTML

所以我不确定在这种情况下我到底做错了什么,因为现在它没有输入任何东西并且由于超时而失败。我希望在此特定元素中输入任何文本。

编辑!

看起来我不得不切换到 iframe,因为后台加载了一个 iframe,这是我无法在字段上实际书写的原因。我不得不使用

browser.switchTo().frame(element(by.xpath("//iframe[@id='cc-integrated-payment-page-frame']")).getWebElement()))

能够在字段内书写。

你的元素ID不一样,<div>pp-cc-first-name-field值,<input>pp-cc-first-name-field值。 尝试按如下方式修复它:

it('Entering First Name', function (done) {

    browser.driver
        .then(() => browser.wait(EC.visibilityOf(element(by.id('pp-cc-first-name'))), 50000, "Timed out finding 'First Name' element"))
        .then(() => element(by.id('pp-cc-first-name')).sendKeys("hello world")
        .then(() => done());
});

要在输入字段中输入 字符序列,您可以使用以下任一方法 :

  • 使用css_selector:

    input#pp-cc-first-name[name='First name'][placeholder='First name']
    
  • 使用xpath:

    //input[@id='pp-cc-first-name' and @name='First name'][@placeholder='First name']
    

实际上,您修改后的代码块将是:

it('Entering First Name', function (done) {

    browser.driver
    .then(() => browser.wait(EC.visibilityOf(element(by.xpath('//input[@id="pp-cc-first-name" and @name="First name"][@placeholder="First name"]'))), 10, "Timed out finding 'First Name' element"))
    .then(() => element(by.xpath('//input[@id="pp-cc-first-name" and @name="First name"][@placeholder="First name"]')).sendKeys("hello world")
    .then(() => done());
});