【问题标题】:unable to switch to iframe using webdriver无法使用 webdriver 切换到 iframe
【发布时间】:2018-12-31 07:06:49
【问题描述】:

我无法切换到 iframe 并对其进行控制。 这是 DOM:

<iframe class="ze_area">
#document
<html><head><link type="text/css" rel="stylesheet" href="https://css.zohostatic.com/ze/v86/css/zeditor.min.css"><style type="text/css">.ze_body{font-family:Verdana,arial,Helvetica,sans-serif;font-size:13px;} table{font-size:100%} .MsoNormal{margin: 0px}</style><style>ol.code li { word-wrap: break-word; } ol.code li pre,span,p,div {white-space: normal !important; }</style>
</head>
<body class="ze_body">
</body></html>
<iframe>

我正在尝试运行此代码

WebElement filebug=driver.findElement(By.id("fileabugnew"));
filebug.click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement filebug_subject=driver.findElement(By.id("fileabug_subject"));
filebug_subject.sendKeys("web erp : test failed to search for web title");
 driver.switchTo().frame(0);
WebElement 
filebug_content=driver.findElement(By.className("//body[@class='ze_body']"));
filebug_content.sendKeys("erp content. the test is failed");
WebElement click_to_assign=driver.findElement(By.xpath("//span[text()='Not 
 Assigned']"));
click_to_assign.click();
 WebElement 
  type_to_assign=driver.findElement(By.xpath("//div[@class='select2-drop 
   searchableselect2 select2-with-searchbox select2-drop- 
   active']//input[@class='select2-input']"));
type_to_assign.sendKeys("shashank.choursia");
driver.findElement(By.id("submitbug")).click();

【问题讨论】:

    标签: java selenium-webdriver iframe browser-automation


    【解决方案1】:

    您需要找到您尝试切换到的 iframe 的索引。 driver.switchTo().frame(0);表示您正在切换到索引为“0”的 iframe。欲了解更多信息,请参阅How to identify and switch to the frame in selenium webdriver when frame does not have id

    WebElement iframe = driver.findElement(By.tagName("iframe"));
    driver.switchTo().frame(iframe);   
    // your action here
    driver.switchTo().defaultContent();
    

    【讨论】:

    • 请分享您遇到的错误。您的 html 代码中还有其他 iframe 吗?我已经用更多信息更新了我的答案
    • 我可以分享网址。你可以充分利用它。做一件事。去这个网址accounts.zoho.com/signin?servicename=ZohoBugTracker并在这里注册一个帐户。它是免费使用的。制作一个演示项目,然后尝试提交问题(提交错误)。我希望这会对你有所帮助。
    • @DieHardCoder,试试我的第二个答案,这对我有用。即通过标记名切换到帧
    • @DieHardCoder,如果解决方案有效,它应该被接受为答案。谢谢
    • 我知道只是尝试先执行代码。 @theGuy
    【解决方案2】:

    最初,您需要导航到父 Frame 以供使用:

    driver.switchto().defaultContent();
    

    完成后,您可以根据索引导航到 webelement 所在的框架。

    driver.switchTo().frame(i);
    

    其中“i”是从“0”开始的索引。

    希望这对您有所帮助,并且让我知道查询。

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 1970-01-01
      • 2017-12-24
      • 2023-03-08
      • 1970-01-01
      • 2021-06-19
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多