【问题标题】:Unable to send keys within To (Email id) field on Compose E-mail page using selenium webdriver无法使用 selenium webdriver 在撰写电子邮件页面上的收件人(电子邮件 ID)字段中发送密钥
【发布时间】:2018-04-04 10:30:26
【问题描述】:

To”按钮无法从 selenium webdriver 获取密钥。在输出中显示无法定位元素。 “To”在 iframe 中,我使用了 I frame,但它也无法正常工作。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class mail {
    public static void main(String[] args) throws InterruptedException  {

        System.setProperty("webdriver.gecko.driver", "D:\mozilla   driver\geckodriver.exe");
        WebDriver driver=new FirefoxDriver();
        driver.get("https://www.mail.com/int/");
        driver.findElement(By.xpath(".//*[@id='login-button']")).click();
        driver.findElement(By.xpath(".//*[@id='login-email']")).sendKeys("rahulrahulxyz@mail.com");
        driver.findElement(By.xpath(".//*[@id='login-password']")).sendKeys("incredible");
        driver.findElement(By.xpath(".//*[@id='login-form']/button")).click();

        driver.switchTo().frame("thirdPartyFrame_home");
        driver.findElement(By.linkText("Compose E-mail")).click();
        Thread.sleep(5000);

        driver.switchTo().frame("thirdPartyFrame_mail");     // **here is error**
        driver.findElement(By.xpath(".//*[@id='idbd']/div[2]/div[1]/div[1]/div[2]/div/div/ul/li/input")).sendKeys("abcde@mail.com");
    }
}

【问题讨论】:

标签: java selenium selenium-webdriver iframe webdriver


【解决方案1】:

要通过sendKeys() 方法发送Emailid,您必须先切换回defaultContent,然后再次切换到预期的frame WebDriverWait 并最终诱导 WebDriverWait 使 To 字段可交互,然后发送 Emailid 如下:

  • 代码块:

    driver.switchTo().defaultContent();
    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("thirdPartyFrame_mail")));
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='compose-header_item compose-header_to mailobjectpanel-objectivation mailobjectpanel-textfield js-component mailobjectpanel']//div[@class='select2-container select2-container-multi js-select2']/ul/li/input"))).sendKeys("abcde@mail.com");
    
  • 快照:

【讨论】:

  • 我了解除 xpath 之外的所有代码。你能解释一下为什么你写这样一个 xpath 以及为什么 firebug xpath 不起作用。使用这种类型的 xpath 将浪费大量时间来自动化整个页面。他们有什么方法可以很容易地取出这种类型的 xpath。 . drive.google.com/open?id=1gdabzuXiaqonzI0ulnP8nkD6P53cAMKE
  • 我写的 xpath 只是模拟了 dom,是一个逻辑 xpath。 Firebug 只会为您提供 absolute xpaths。因此,您必须逐渐从使用 absolute xpaths 转向 logical xpath,这将提供急需的稳定性。
  • 为什么不使用简单的 XPath,例如://*[@id='idbd']//input
  • @DebanjanB 非常感谢。我的问题现在解决了。
  • @Frank 有多个元素。为了使 xpath 独一无二,我必须这样做。
猜你喜欢
  • 2017-02-08
  • 2016-05-02
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
相关资源
最近更新 更多