【问题标题】:i want to write in textbox if it is display on page and click on next button if it is not display on the page then directly click on next button如果它显示在页面上,我想在文本框中写入,如果页面上没有显示,则单击下一步按钮,然后直接单击下一步按钮
【发布时间】:2015-09-10 12:00:00
【问题描述】:
if(!driver.findElement(By.id("pt1:r1:0:r1:2:r1:0:it1::content")).isEnabled())
{   
    System.out.println("IS NOT DISPLAYED"); // if text box is display
    driver.findElement(By.id("pt1:r1:0:r1:2:next")).click();
}
else if(driver.findElement(By.id("pt1:r1:0:r1:2:r1:0:it1::content")).isEnabled())
{           
    System.out.println("IS DISPLAYED"); // if text box is not display
    driver.findElement(By.id("pt1:r1:0:r1:2:r1:0:it1::content")).sendkey("ABCXYZ");
    driver.findElement(By.id("pt1:r1:0:r1:2:next")).click();
}

我已尝试使用 try 和 catch 阻止它的工作,但执行脚本需要时间。

【问题讨论】:

  • 我不太明白你在寻求什么帮助。上面的代码有没有给你任何错误?您在哪里尝试插入 try/catch 并提供了任何信息或线索说明可能出现的问题?
  • 他说上面的代码运行良好,但花费了太多时间。要求更好的解决方案或对代码进行一些修改以提高其性能

标签: java selenium selenium-webdriver


【解决方案1】:

简单解释一下,你要做的是: 1.检查页面上是否存在文本框...如果存在,则将文本放入其中。 2. 点击下一步。

无论复选框是否存在,您仍然单击下一个按钮,以便将其拉到您的 if 之外。

List<WebElement> textboxes = driver.findElements(By.id("pt1:r1:0:r1:2:r1:0:it1::content"));
if (!textboxes.isEmpty())
{
    // textbox exists
    textboxes.get(0).sendKeys("ABCXYZ");
}

driver.findElement(By.id("pt1:r1:0:r1:2:next")).click();

如果您要重用定位器 (Bys),我建议您将它们存储在变量中,例如

By nextButtonLocator = By.id("pt1:r1:0:r1:2:next");
driver.findElement(nextButtonLocator).click();

我建议您阅读一些有关 Java 编程的教程以提高您的编程知识。您还可以获得一些非常好的 Java 书籍。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    相关资源
    最近更新 更多