如何在文本框中找到并键入内容

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 wait = new FluentWait(driver) .withTimeout(10, TimeUnit.SECONDS) .pollingEvery(100, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class); WebElement element = wait.until(new Function(){ public WebElement apply(WebDriver drv){ return drv.findElement(By.id(locator)); } }); return element; } } 

我无法找到并以错误结束。

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

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

等待你可以使用这样的东西

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

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

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

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

如果需要,还可以更改定位器类型并增加/减少时间限制

我希望这有助于你..

 import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class yahoo { FirefoxDriver Driver=null; WebDriverWait wait = null; @BeforeTest public void start(){ Driver = new FirefoxDriver(); } @Test public void Test() throws InterruptedException{ wait=new WebDriverWait(Driver,90); System.out.println("Loading yahoo search page"); Driver.get("http://www.yahoo.com"); System.out.println("Yahoo search page loaded fine"); WebElement text=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='p_13838465-p']"))); text.sendKeys("selenium"); Thread.sleep(5000); } @AfterTest public void close(){ Driver.quit(); } } 

关于你的By.id语句,看起来你已经传递了XPath而不是id。 所以这:

 element = myDynamicElement(By.id("//*[@id='p_13838465-p']")); //this locator is an XPath 

应该成为这样的:

 element = myDynamicElement(By.id('p_13838465-p')); //this is just the ID 

但是,如果动态生成此ID,则它将无法可靠地工作,您可能需要考虑通过其他定位器查找它。

然后,一旦你确定了元素,要输入它,你就可以使用.sendKeys("your text here") ,如下所示:

 element.sendKeys("your text here"); 

或者你可以通过跳过element = part将它组合成一行,然后说:

 myDynamicElement(By.id('p_13838465-p')).sendKeys("your text here"); 

我遇到了同样的问题,找不到任何解决方案。然后我尝试了WebDriverWait而不是FluentWait,它对我有用

 WebDriverWait wait = new WebDriverWait(Driver, 60); wait.withTimeout(60, TimeUnit.SECONDS); wait.pollingEvery(5, TimeUnit.SECONDS); wait.ignoring(NoSuchElementException.class); wait.until(new ExpectedCondition(){ @Override public Boolean apply(WebDriver driver) { WebElement ele=driver.findElement(locator); if(ele==null) return false; else { System.out.println("WebElement found"); return true; } } });