如何在文本框中定位和输入内容

How to locate and type something in the textbox(如何在文本框中定位和输入内容)
本文介绍了如何在文本框中定位和输入内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class testFluent {   

WebDriver driver;   
    @Before
        public void setUp(){        
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();}

         @Test
        public void myFirstFluent(){
        WebElement element;
        driver.get("http://www.yahoo.com");         
        element = myDynamicElement(By.id("//*[@id='p_13838465-p']"));
        System.out.println("Element found");
        }

        public WebElement myDynamicElement(final By locator){

        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(10, TimeUnit.SECONDS)
                    .pollingEvery(100, TimeUnit.MILLISECONDS)
                    .ignoring(NoSuchElementException.class);

            WebElement element = wait.until(new Function<WebElement, WebDriver>(){

                public WebElement apply(WebDriver drv){
                    return drv.findElement(By.id(locator));
                }
            });

                return element;
        }

    }

我无法定位并以错误结尾.

I am unable to locate and ends with Error.

java.lang.Error: 未解决的编译问题:Wait 类型中的方法 until(Function) 不适用于参数 (new Function(){}) 函数无法解析为类型

java.lang.Error: Unresolved compilation problems: The method until(Function) in the type Wait is not applicable for the arguments (new Function(){}) Function cannot be resolved to a type

类型By中的方法id(String)不适用于com.junit.qa.testFluent.myDynamicElement(testFluent.java:49)处的参数(By)

The method id(String) in the type By is not applicable for the arguments (By) at com.junit.qa.testFluent.myDynamicElement(testFluent.java:49)

推荐答案

等一下你可以用这样的东西

For wait you can use something like this

    private boolean wAit(String match)
    {
    try
        {
        (new WebDriverWait(driver, 30))
            .until(ExpectedConditions.presenceOfElementLocated (By.xpath(match)));
        return true;
        }
    catch (NoSuchElementException e) {
        return false;
    }
    }

您可以创建上述方法并在需要等待元素的任何地方使用它.例如

You can create the above method and use it where ever you need to wait for an element. for example

如果想在文本框中写一些东西并想等待文本框加载

if want a write something in the textbox and want to wait for the text box to load

wAit(" xpath of the textbox here")
driver.findelements... sendkeys()..

如果您愿意,您可以更改定位器类型并增加/减少时间限制

If you want you can change the locator type and increase/decrease the time limit also

这篇关于如何在文本框中定位和输入内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)