当findElement函数无法找到文本框时,如何将key发送到文本框? findElement还有其他选择吗?

时间:2016-11-14 06:25:14

标签: selenium selenium-webdriver

当findElement函数无法找到文本框时,如何将key发送到文本框? findElement还有其他选择吗?

这是一个在采访中提出的问题

4 个答案:

答案 0 :(得分:1)

您也可以使用JavascriptExecutor执行相同操作,

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('someid').value=arguments[0]","text to be inserted in input box");

答案 1 :(得分:1)

在这种情况下,您可以尝试使用JavascriptExecutor

例如。要在Google搜索文本字段中输入文本,以下代码应该有效:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('gbqfq').value = 'Ripon Al Wasim';");

参考网址:How to input a value in a text field/box by using JavasSript in Selenium WebDriver

答案 2 :(得分:0)

也许试试这个剧本:

WebDriverWait wait = new WebDriverWait(driver, 1);         
wait.until(ExpectedConditions.elementToBeClickable(By.id("element's id here"))).click();
 wait.until(ExpectedConditions.presenceOfElementLocated(By.id("element's id here"))).sendKeys("enter text you want to put in");

答案 3 :(得分:-1)

是的,我们有。使用密钥,我们可以在文本框中插入文本。但是在将值插入文本框之前,我们需要检查文本框之前的元素,然后按Tab键跳转到文本框。

检查元素定位器值:

d.findElement(By.xpath("")).sendKeys(Keys.TAB,"enter your text");

输入的文字将插入到您的文本框中。