【问题标题】:How to get the title from an iframe element using selenium?如何使用 selenium 从 iframe 元素中获取标题?
【发布时间】:2021-09-04 00:33:18
【问题描述】:

我正在尝试从这样的 iframe 标记中获取标题:

const webdriver = require('selenium-webdriver');
const {  By, Key, until } = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');

let driver;

async function whenElement(by_identity,timeout=1000){
  try{
  var el = await driver.findElement(by_identity);
  await driver.wait(until.elementIsVisible(el),timeout);
  return el;
  }
  catch(err){
    console.log(`whenElement,ELEMENT ${by_identity} not found, ERROR=>${err}`);
  }
} 


(async ()=>{
  driver=await new webdriver.Builder().forBrowser('firefox').build();
  await driver.get(`https://www.google.com/recaptcha/api2/demo`);
  await driver.getTitle().then(text=>{console.log(text)});

  await driver.switchTo().frame(await whenElement(By.xpath('/html/body/div[1]/form/fieldset/ul/li[5]/div/div[1]/div/div/iframe'),5000))
  await driver.getTitle().then(text=>{console.log(text)});

})();

预期的输出应该是:

reCAPTCHA demo
reCAPTCHA

但我只得到主页的标题而不是 iframe,尽管像这样切换驱动程序的框架:

reCAPTCHA demo
reCAPTCHA demo

我不确定我做错了什么
也许我对更改驱动程序框架的理解是错误的?因为据我所知,如果你 driver.switchTo().frame() 那么驱动程序只能在该框架内运行,并且不能与其他框架或主 html DOM 中的其他元素交互

谁能解释一下是什么问题?

【问题讨论】:

    标签: javascript node.js selenium selenium-webdriver


    【解决方案1】:

    你不需要切换到框架来获取标题。相反,您可以使用它:

    var frameElm = await driver.findElement(By.xpath('.//iframe[1]'));
    console.log(frameElm.getAttribute("title"));
    

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多