Java 无法使用selenium在文本框中插入文本

Java 无法使用selenium在文本框中插入文本,java,selenium,selenium-webdriver,xpath,css-selectors,Java,Selenium,Selenium Webdriver,Xpath,Css Selectors,无法使用selenium定位文本框。错误如下: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"proposedTagName"} HTML: 新标记名: 您可以使用driver.findElement(By.id(“proposedTagName”)).sendKeys(“valuet”) 像这样的- imp

无法使用selenium定位文本框。错误如下:

 org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"proposedTagName"}
HTML:


新标记名:

您可以使用
driver.findElement(By.id(“proposedTagName”)).sendKeys(“valuet”)

像这样的-

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class A {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", chromeDriverPath); //the path of chrome exe here
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, 10);
        driver.get("url here");
        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("proposedTagName")));
        element.sendKeys("value");
    }

}

可能您有时需要等待,然后才能设置如下所示的值

WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("proposedTagName")));
element.sendKeys("valuetobesend");

此外,请检查元素是否在框架内,如果在框架内,则需要切换到之前的框架。

请使用下面的xpath以及预期条件

XPath:

//table//input [@id='proposedTagName']
String value="XXXXX";
WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table//input[@id='proposedTagName']")));
driver.findElement(By.xpath("//table//input[@id='proposedTagName']")).sendKeys(value);
代码:

//table//input [@id='proposedTagName']
String value="XXXXX";
WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table//input[@id='proposedTagName']")));
driver.findElement(By.xpath("//table//input[@id='proposedTagName']")).sendKeys(value);

根据HTML,您已共享元素
{“方法”:“id”,“选择器”:“proposedTagName”}
是一个
标记,您可能需要向元素发送字符序列。要实现这一点,您可能需要引导WebDriverWait使所需元素可单击,并且您可以使用以下解决方案:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//form[@id='addTagForm']//following::table[1]//input[@class='textInput baselineVersionInput' and @id='proposedTagName']"))).sendKeys("A.Prakash");

我已经使用过://$.driver().get().findElement(By.xpath(“//input[@id='proposedTagName']”);JavascriptExecutor executor=(JavascriptExecutor)$.driver().get();executor.executeScript(“document.getElementById('proposedTagName')。value='Aman”)$(“提议的标签名称”).val(“标签测试1”);检查元素是否在框架内?如果有id的xath,而不直接使用
By.id
,有什么好处?@eduPeeth关于stackoverflow的讨论很多,关于您提出的问题。也许这个答案的方向是正确的。@DebanjanB,如果你能分享一下这个问题的答案,我将不胜感激。我无法在这个方向找到一个。@eduPeeth您可以根据您的要求提出一张罚单吗?@DebanjanB,我可以,但仍然不相信id为的xpath比上面仅id为的xpath具有优先权(优势)。会挖得更深。这不是单击元素的问题,而是知道为什么will By.id不起作用,而带id的xpath会起作用,这是我希望看到的原因。它显示错误:org.openqa.selenium.WebDriverException:未知错误:a.tagName.toUpperCase不是函数