元素在Selenium中未找到异常

元素在Selenium中未找到异常,selenium,selenium-webdriver,xpath,webdriver,Selenium,Selenium Webdriver,Xpath,Webdriver,我正在尝试向内联弹出窗口中的字段发送键。公司名称、联系人姓氏和联系人姓名。没有iFrame。我也尝试了windowhandler,但没有成功。这是我的代码片段,请在下面找到HTML代码: driver.findElement(By.xpath("//input[@name='company_name']")).sendKeys("Ginger Bread"); driver.findElement(By.name("contact_first_name")).sendKeys("Ingrid")

我正在尝试向内联弹出窗口中的字段发送键。公司名称、联系人姓氏和联系人姓名。没有iFrame。我也尝试了windowhandler,但没有成功。这是我的代码片段,请在下面找到HTML代码:

driver.findElement(By.xpath("//input[@name='company_name']")).sendKeys("Ginger Bread");
driver.findElement(By.name("contact_first_name")).sendKeys("Ingrid");
driver.findElement(By.name("contact_surname")).sendKeys("Cumberbridge");
HTML代码:

<div id="ibox_content" style="overflow: auto; height: 322px;"><div id="quickCreateForm" style="background:silver; padding:4px; border:1px solid white; width:95%;">
<form> 
    <table width="100%" border="0" cellpadding="2" cellspacing="0">
    <tbody><tr><td colspan="2" align="center">
    <input type="button" value="Create" class="button" onclick="return quickCreate(this, true); ">

    <input type="button" value="Close" class="button" onclick="iBox.hide();">

    </td></tr>
    <tr><td align="right" valign="top"><strong>Company:</strong></td>
        <td align="left" valign="top" nowrap="">  
        <input type="text" name="company_name" value="" size="15" maxlength="120">
    </td></tr>
    <tr><td align="right" valign="top"><strong>First Name:</strong></td>
        <td align="left" valign="top"> 
        <input type="text" name="contact_first_name" value="" size="15" maxlength="120">
    </td></tr> 
    <tr><td align="right" valign="top"><strong>Last Name:</strong></td>
        <td align="left" valign="top">
        <input type="text" name="contact_surname" value="" size="15" maxlength="120"> 
        </td></tr>

    </tbody></table>
</form>
</div></div>

公司:
名字:
姓氏:
尝试使用CSS选择器 driver.findelelement(By.CssSelector(“输入[name=company_name]”)。sendKeys(“姜饼”); driver.findElement(由.CssSelector(“输入[name=contact\u first\u name]”)创建)。sendKeys(“Ingrid”);
driver.findelelement(由.CssSelector(“输入[姓名=联系人姓名]”))).sendKeys(“Cumberbridge”)

下面的xpath应该可以帮助您:


//div[@id='ibox\u content'和contains(@style,'height')]//input[@name=“company\u name”]

尝试使用下面给出的xpath:

driver.findElement(By.xpath("//input[contains(@name,'company_na')]")).sendKeys("Ginger Bread");
您可以使用以下命令调用所需字段的
sendKeys()

  • 公司

    driver.findElement(By.xpath("//div[@id='quickCreateForm']/form//td/strong[contains(.,'Company')]//following::td[1]/input")).sendKeys("Ginger Bread");
    
  • 名字

    driver.findElement(By.xpath("//div[@id='quickCreateForm']/form//td/strong[contains(.,'First Name')]//following::td[1]/input")).sendKeys("Ingrid");
    
    driver.findElement(By.xpath("//div[@id='quickCreateForm']/form//td/strong[contains(.,'Last Name')]//following::td[1]/input")).sendKeys("Cumberbridge");
    
  • 姓氏

    driver.findElement(By.xpath("//div[@id='quickCreateForm']/form//td/strong[contains(.,'First Name')]//following::td[1]/input")).sendKeys("Ingrid");
    
    driver.findElement(By.xpath("//div[@id='quickCreateForm']/form//td/strong[contains(.,'Last Name')]//following::td[1]/input")).sendKeys("Cumberbridge");
    

看起来您在加载实际元素之前发送密钥,所以 在尝试向其发送密钥之前,需要显式等待

WebElement element = new WebDriverWait(Driver,30).until(ExpectedConditions.elementToBeClickable(By.name("company_name")));
或者你可以用这个

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
然后打电话,

driver.findElement(By.xpath("//input[contains(@name,'company_name')]")).sendKeys("Ginger Bread");

请通读如何提供。目前的问题是,很难回答。为什么用部分的
@name
搜索比用精确的
@name
搜索更有效?