【问题标题】:Entering text in pop up and saving it on TestNG在弹出窗口中输入文本并将其保存在 TestNG 中
【发布时间】:2014-01-27 16:32:11
【问题描述】:

我是自动化测试的新手。我正在使用 twitter 作为学习 selenium 的测试网站。

我想创建一条新推文,为此我正在使用以下场景。我可以打开“新推文”弹出窗口,但仍然无法在其文本框中输入文本。请帮忙。

先决条件:登录 Twitter

  1. 单击屏幕右上角的按钮(用于创建新推文)。 弹出窗口打开
  2. 单击弹出窗口中的文本框输入文本。 光标在文本框中突出显示并启用保存按钮
  3. 输入一些文字
  4. 点击保存按钮

对我来说,它一直工作到第 2 点,之后它不会在文本框中输入文本

请参阅下面的代码:

String popUpHandle = driver.getWindowHandle(); 

driver.switchTo().window(popUpHandle);

driver.findElement(By.id("global-new-tweet-button")).click();

driver.findElement(By.xpath("(//button[@type='button'])[123]")).click();

driver.findElement(By.xpath("(//button[@type='button'])[123]")).sendKeys("test tweet");

driver.findElement(By.xpath("//button[@type='button'])[106]")).click();

【问题讨论】:

  • 会抛出异常吗?

标签: selenium popup window


【解决方案1】:

@user3241182

我只是自己测试了这个。您不需要使用任何窗口处理。 下面的代码是提供的解决方案,效果很好。

    WebDriver driver = new FirefoxDriver();
    driver.get("https://twitter.com/");
    WebElement username = driver.findElement(By.id("signin-email"));
    username.clear();
    username.sendKeys("your_twitter_handle");
    WebElement pass = driver.findElement(By.id("signin-password"));
    pass.clear();
    pass.sendKeys("your_twitter_passwd");
    WebElement signInButton = driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]/div[2]/div[2]/form/table/tbody/tr/td[2]/button"));
    signInButton.click();
    WebElement tweetButton = driver.findElement(By.id("global-new-tweet-button"));
    tweetButton.click();
    WebElement tweetBox = driver.findElement(By.id("tweet-box-global"));
    tweetBox.clear();
    tweetBox.sendKeys("This is an automated tweet!");
    WebElement tweetSubmit = driver.findElement(By.xpath("/html/body/div[9]/div[2]/div[2]/div[2]/form/div[2]/div[2]/button"));
    tweetSubmit.click();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-05
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多