Selenium中找不到元素异常

时间:2018-10-09 20:03:40

标签: selenium selenium-webdriver xpath webdriver

我正在尝试将Keys发送到嵌入式弹出窗口中的字段。 company_name,contact_surname和contact_first_name。没有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>

5 个答案:

答案 0 :(得分:1)

尝试使用CSS选择器 driver.findElement(By.CssSelector(“ input [name = company_name]”))。sendKeys(“ Ginger Bread”);     driver.findElement(By.CssSelector(“ input [name = contact_first_name]”))。sendKeys(“ Ingrid”);     driver.findElement(By.CssSelector(“ input [name = contact_surname]”))。sendKeys(“ Cumberbridge”);

答案 1 :(得分:0)

下面的xpath应该会帮助您:

//div[@id='ibox_content' and contains(@style,'height')]//input[@name="company_name"]

答案 2 :(得分:0)

尝试使用下面给出的xpath:

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

答案 3 :(得分:0)

您可以使用以下Locator Strategies来调用sendKeys()到所需字段:

  • Company

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

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

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

答案 4 :(得分:0)

似乎您是在加载实际元素之前发送密钥的,所以 在尝试向其发送密钥之前,需要给予明确的等待,

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");