如何使用Selenium VBA在iframe中的用户名字段中发送文本

如何使用Selenium VBA在iframe中的用户名字段中发送文本

问题描述:

我正在尝试登录链接的网站.它看起来很简单,我已经在其他网站上成功做到了.我可以手动登录.我的代码是

I am attempting to login in the linked website. It looks straightforward and I have done this successfully in other websites. I can login manually. My code is

Dim bot As New WebDriver
bot.Start "chrome", "https://mbwebedi.mahle.com"
bot.Get "/webedi/jsp/index.jsp"
bot.FindElementByName("Login").SendKeys "ABC123"

我收到错误:

"NoSuchElementError Element not found for Name=Login"

元素的文本为:

<input class="loginInput" type="text" name="Login" maxlength="30">`

这时我可以手动进行,所以我认为重点不是问题.我在做什么错了?

I can proceed manually at this point, so I don't think focus is the issue. What I am doing wrong?

要发送字符序列 Username 字段,因为所需元素位于因此您必须:

To send a character sequence to the Username field as the the desired element is within an <iframe> so youhave to:

  • 第一个SwitchToFrame.
  • 然后找到元素.
  • 您可以使用以下定位器策略:

  • 使用FindElementByCss:

bot.SwitchToFrame "liefercontent"
bot.FindElementByCss("input.loginInput[name='Login']").SendKeys "ABC123"

  • 使用FindElementByXPath:

    bot.SwitchToFrame "liefercontent"
    bot.FindElementByXPath("//input[@class='loginInput' and @name='Login']").SendKeys "ABC123"
    

  • 您可以在元素没有找到相关的讨论虽然具有ID属性,但不存在


    tl;博士

    在iframe下处理#document的方式


    tl; dr

    Ways to deal with #document under iframe